PHP编辑不工作

PHP编辑不工作

问题描述:

编辑表中的字段对我来说变得很头疼,因为我找不出简单的解决方案。我的眼睛找不到我的问题原因。数据库功能的更新不起作用。

这是我的PHP为viewproducts.phpPHP编辑不工作

<?php 
    $result = mysql_query("SELECT * FROM products ") 
       or die(mysql_error()); ; 

    if (mysql_num_rows($result) == 0) { 
     echo 'There Arent Any Products'; 
    } else { 

     echo "<table border='1' width=100%><tr><th>Product Name</th><th>Description</th><th>Price</th><th>Image</th><th>Edit</th><th>Delete</th>"; 
     while($info = mysql_fetch_array($result)) 
     { 
      echo "<tr>"; 
      echo "<td>" . $info['name']. "</td>"; 
      echo "<td>" . $info['description']. "</td>"; 
      echo "<td>£" .$info['price']." </td>"; 
      echo "<td>" . "<img src='../getImage.php?id=" . $info['serial'] ."'/></td>"; 

      echo '<td> <a href="edit.php?product_id=' . $info['serial'] . '">Edit</a></td>'; 
     } 
    } 
    echo "</tr>"; 
    echo "</table>"; 
?> 

这里是我的edit.php页:

<?php 
    $id = $_GET['product_id']; 
    $query = mysql_query("SELECT * FROM products WHERE serial = '$id'") 
       or die(mysql_error()); 

    while($info = mysql_fetch_array($query)) { 
     echo ""; 

     $name = $info['name']; 
     $description = $info['description']; 
     $price = $info['price']; 
     $picture = $info['picture']; 
    } 
?> 

<form action="editsuccess.php" method="post"> 

Product ID:<br/> 
<input type="text" value="<?php echo $id;?>" name="serial" disabled/> 

<br/> 

Name:<br/> 
<input type="text" value="<?php echo $name;?>" name="name"/> 

<br/> 

Description:<br/> 
<input type="text" value="<?php echo $description;?>" name="description"/> 

<br/> 

Price:<br/> 
<input type="text" value="<?php echo $price;?>" name="price"/> 

<br/> 

Picture:<br/> 
<? echo'<img src="../getImage.php?id=' . $info['serial'] .'"/>'?> 

</br> 

<input type="submit" value="Update Product"/> 

</form> 

最后这里是我的editsuccess.php页:

<?php 
    session_start(); 
    require_once '../includes/db.php'; 
    $id = $_REQUEST['product_id']; 

    $name = $_POST['name']; 
    $description = $_POST['description']; 
    $price = $_POST['price']; 

    $info = "UPDATE products SET name='$name', description ='$description', price ='$price' WHERE serial ='$id'"; 

    mysql_query($info) or die ("Error: ".mysql_error()); 

    echo "Database updated."; 

?> 

任何帮助家伙?

+0

[**请不要在新代码中使用'mysql_ *'函数**](http://bit.ly/phpmsql)。他们不再被维护,[弃用过程](http://j.mp/Rj2iVR)已经开始。看到[**红框**](http://j.mp/Te9zIL)?学习[*准备的语句*](http://j.mp/T9hLWi),并使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [这篇文章](http://j.mp/QEx8IB)将帮助你决定哪个。如果你选择PDO,[这里是一个很好的教程](http://j.mp/PoWehJ)。 –

它看起来像被发送您没有将product_id传递到您的editsuccess.php页面。试着改变你的<form>声明的第一行中edit.php到:

<form action="editsuccess.php?product_id=<?php echo $id; ?>" method="post"> 
+0

感谢clive,它现在的工作!我还有一个问题。我怎么不能让图像显示与下面的代码?我把它在我的其他页面上显示出来,但不在这个页面上..... '?> – Jahed

+0

我认为是因为你将'$ info'设置为你的'mysql_query'的结果,它不会像你所期望的那样是一个记录数组。我猜你需要使用'$ _POST ['serial']'而不是 – Clive

+0

,没有像这样的东西,因为我说我得到它在另一个页面上使用相同的代码行。你是否希望我编辑我的第一篇文章并发布工作页面的代码和不工作的页面? – Jahed

变化

<input type="text" value="<?php echo $id;?>" name="serial" disabled/> 

<input type="text" value="<?php echo $id;?>" name="product_id" disabled/> 

你也应该删除从外地disabled属性,因为一个无效字段将不会在表单提交

它看起来像你没有连接。我看不到连接...