0

我正在尝试创建一个表单来编辑我的数据库表中的行。(基于我从 StackOverflow 页面获得的一些代码。)

我可以使用相关数据填充表单,但是当我提交表单时,该行没有更新。事实上,我的一些专栏被删除了。

我做错什么了?

编辑.php

<?php

$UID = (int)$_GET['f'];
$query = mysql_query("SELECT * FROM user_feeds WHERE feed_id = '$UID'") or die(mysql_error());

if(mysql_num_rows($query)>=1){
    while($row = mysql_fetch_array($query)) {
        $feedtitle = $row['feed_title'];
        $feedurl = $row['feed_url'];
        $feedorder = $row['feed_order'];
        $feedowner = $row['feed_owner'];
    }
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?=$UID;?>">
Title:<br /> <input type="text" name="ud_feedtitle" value="<?=$feedtitle?>"><br>
URL: <br /> <input type="text" name="ud_feedurl" value="<?=$feedurl?>"><br>
Order: <br /> <input type="text" name="ud_feedorder" value="<?=$feedorder?>"><br>
Owner:<br /> <input type="text" name="ud_feedowner" value="<?=$feedowner;?>"><br>
<input type="Submit">
</form>
<?php
}else{
    echo 'No entry found. <a href="javascript:history.back()">Go back</a>';
}
?>
</div>
</body>
</html>

更新.php

    <?php



$ud_ID = $_REQUEST["ID"];
$ud_feedtitle = $_POST["feed_title"];
$ud_feedurl = $_POST["feed_url"];
$ud_feedorder = $_POST["feed_order"];
$ud_feedowner = $_POST["feed_owner"];


$query = "UPDATE user_feeds SET feed_title = '$ud_feedtitle', feed_url = '$ud_feedurl', feed_order = '$ud_feedorder', feed_owner = '$ud_feedowner', WHERE feed_id = '$ud_ID'";


$res = mysql_query($query);

if ($res)
  echo "<p>Record Updated<p>";

else
  echo "Problem updating record. MySQL Error: " . mysql_error();

?>
4

1 回答 1

2

原因:

输入字段的名称与填充$_POST的名称相同。您当前请求的变量:

$_POST["feed_title"];, $_POST["feed_url"];,  $_POST["feed_order"];, $_POST["feed_owner"];

都是空的,因为它们不存在。更新时,您将表中的值替换为空白值。


解决方案:

在您update.php,的以下内容中应该存在。

$ud_ID = $_POST["ID"];
$ud_feedtitle = $_POST["ud_feedtitle"]; //corresponding to <input type="text" name="ud_feedtitle" ...
$ud_feedurl = $_POST["ud_feedurl"]; //corresponding to <input type="text" name="ud_feedurl" ...
$ud_feedorder = $_POST["ud_feedorder"]; //corresponding to <input type="text" name="ud_feedorder" ...
$ud_feedowner = $_POST["ud_feedowner"]; //corresponding to <input type="text" name="ud_feedowner" ...
于 2012-07-29T14:57:00.300 回答