我的代码出了什么问题?我可以显示我的数据库中的文本,但更新不起作用..
的index.php
$i=0;
while ($i < $num) {
$first=mysql_result($result,$i,"FirstName");
$last=mysql_result($result,$i,"LastName");
$age=mysql_result($result,$i,"Age");
echo "<b>$first $last</b><br>Age: $age<br>";
$i++;
}
$query="SELECT * FROM persons WHERE personID='$id'";
$resulta=mysql_query($query);
$list=mysql_numrows($resulta);
$i=0;
while ($i < $list) {
$first=mysql_result($resulta,$i,"FirstName");
$last=mysql_result($resulta,$i,"LastName");
$age=mysql_result($resulta,$i,"Age");
++$i;
}
?>
<form action="update.php">
<input type="hidden" name="ud_id" value="<?php echo "$id"; ?>">
First Name: <input type="text" name="ud_first" value="<?php echo "$first"?>"><br>
Last Name: <input type="text" name="ud_last" value="<?php echo "$last"?>"><br>
Age: <input type="text" name="ud_age" value="<?php echo "$age"?>"><br>
<input type="Submit" value="Update">
</form>
update.php
<?php
include_once('config.php');
$query="UPDATE persons SET FirstName='$ud_first', LastName='$ud_last', Age='$ud_age' WHERE personID='$ud_id'";
mysql_query($query);
?>
答案 0 :(得分:2)
我认为问题是http://lt.php.net/manual/en/security.globals.php
所以你不能使用$ud_id
你必须这样做$_POST['ud_id']
HTML:
<form action="update.php" method="post">
PHP:
$ud_id = mysql_real_escape_string($_POST['ud_id']);
...
答案 1 :(得分:0)
我认为你唯一忘记的是在你的while函数中获取id,以及获取你需要在update.php中使用$ _GET的id 我希望这会给你一点启动。
表示index.php
$i=0;
while ($i < $list)
{
$id =mysql_result($resulta,$i,"id");
$first=mysql_result($resulta,$i,"FirstName");
$last=mysql_result($resulta,$i,"LastName");
$age=mysql_result($resulta,$i,"Age");
++$i;
}
for update.php
include_once('config.php');
$ud_id = $_GET['id'];
$query="UPDATE persons SET FirstName='$ud_first', LastName='$ud_last', Age='$ud_age' WHERE personID='$ud_id'";
if(!$query)
{
die('Could not delete data: ' . mysql_error());
}
mysql_close($link);