PHP编辑不起作用

时间:2011-11-24 23:00:33

标签: php mysql error-handling

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

这是我的PHP for viewproducts.php

<?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.";

?>

任何帮助人员?

3 个答案:

答案 0 :(得分:2)

您似乎没有将product_id传递到editsuccess.php页面。尝试将<form>edit.php声明的第一行更改为:

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

答案 1 :(得分:1)

更改

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

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

您还应该从字段中删除disabled属性,因为表单提交中不会发送已禁用的字段

答案 2 :(得分:0)

看起来你没有被联系。我看不到连接...