我正在尝试更新数据库中的数据,但是当我转到编辑页面时它们不会显示。有帮助吗?谢谢。
的index.php
<?php
include_once('db.php');
if(isset($_POST['description']))
{
$description = $_POST['description'];
if(mysql_query("INSERT INTO about VALUES('','$description')"))
echo "Sucacessful Update";
else
echo "Please try again";
}
$res = mysql_query("SELECT * FROM about");
?>
<form action = "." method="POST">
Name <input type = "text" name="description"><br/>
<input type="submit" value="Save">
</form>
<?php
while( $row = mysql_fetch_array($res) )
echo "$row[id]. $row[description] <a href='edit.php?edit=$row[description]'>edit</a><br />";
?>
edit.php
<?php
include_once('db.php');
if( isset($_GET['edit']) )
{
$id = $_GET['edit'];
$res= mysql_query("SELECT * FROM about WHERE id='$id'");
$row= mysql_fetch_array($res);
}
if( isset($_POST['newDescription']) )
{
$newDescription = $_POST['newDescription'];
$id = $_POST['id'];
$sql = "UPDATE about SET description='$newDescription' WHERE id='$id'";
$res = mysql_query($sql)
or die("Could not update".mysql_error());
echo "<meta http-equiv='refresh' content='0;url=index.php'>";
}
?>
<form action="edit.php" method="POST">
Name: <input type="text" name="newDescription" value="<?php echo $row[1]; ?>"><br />
<input type="hidden" name="id" value="<?php echo $row[0]; ?>">
<input type="submit" value=" Update "/>
</form>
我的表中的名称是关于,我有两列是id和description。我在这里尝试做的是通过php编辑描述列。
答案 0 :(得分:1)
将行id
而不是description
传递给edit.php
脚本,所有内容都会更好用
<?php
while( $row = mysql_fetch_array($res) )
echo "$row[id]. $row[description] <a href='edit.php?edit=$row[id]'>edit</a><br />";
?>
请不要使用
mysql_
数据库扩展,不推荐使用(在PHP7中永远不会使用) 特别是如果您只是学习PHP,请花时间学习PDO
或mysqli_
数据库扩展, and here is some help to decide which to use