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.";
?>
任何帮助家伙?
答
它看起来像被发送您没有将product_id
传递到您的editsuccess.php
页面。试着改变你的<form>
声明的第一行中edit.php
到:
<form action="editsuccess.php?product_id=<?php echo $id; ?>" method="post">
答
变化
<input type="text" value="<?php echo $id;?>" name="serial" disabled/>
到
<input type="text" value="<?php echo $id;?>" name="product_id" disabled/>
你也应该删除从外地disabled
属性,因为一个无效字段将不会在表单提交
答
它看起来像你没有连接。我看不到连接...
[**请不要在新代码中使用'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)。 –