1

我知道,我的问题在于我的where语句中没有唯一标识符,应该只更新哪一行。这是我要说的更清楚的内容:

<?php        
$res=mysql_query("select * from orders where feedback_reminder='1' ");
while($row=mysql_fetch_array($res))
{ ?>
    <form method="post">
        <div><input name="ship_date" type="text" id="ship_date" value="<?php echo $row['ship_date']; ?>" /></div>
        <div><input name="feedback_reminder" type="text" id="feedback_reminder" value="<?php echo $row['feedback_reminder']; ?>" /></div>
        <div><input type="submit" name="button" id="button" value="Update" class="submit" onMouseOver="this.className='submit submithov'" onMouseOut="this.className='submit'"/></div>
    </form>

    <?php
    if(count($_POST)>0)
    {
        mysql_query("update orders set
        ship_date='".$_POST["ship_date"]."',
        feedback_reminder='".$_POST["feedback_reminder"]."'
        where id='".$row['id']."' ");
    }
}
?>

很明显,这不起作用,因为where id='".$row['id']."'对于orders表中的所有行都是正确的,这就是为什么我的整个表会不断更新。我该如何进行这项工作,以便只有在我按下更新按钮的地方才能更新行?我希望我已经足够清楚了,谢谢你的帮助!

4

1 回答 1

1

用隐藏字段做

$res=mysql_query("select * from orders where feedback_reminder='1' ");
while($row=mysql_fetch_array($res))
{ ?>
<form method="post">
    <div><input name="ship_date" type="text" id="ship_date" value="<?php echo $row['ship_date']; ?>" /></div>
    <div><input name="feedback_reminder" type="text" id="feedback_reminder" value="<?php echo $row['feedback_reminder']; ?>" /></div>
    <input name="id" type="hidden" value="<?php echo $row['id']; ?>" />
    <div><input type="submit" name="button" id="button" value="Update" class="submit" onMouseOver="this.className='submit submithov'" onMouseOut="this.className='submit'"/></div>
</form>

<?php
if(isset($_POST['button']))
 {
    mysql_query("update orders set
    ship_date='".$_POST["ship_date"]."',
    feedback_reminder='".$_POST["feedback_reminder"]."'
    where id='".$_POST['id']."' ");
 }
}

更新记录后使用重定向。

于 2013-09-11T12:11:03.717 回答