MySql查询返回多个结果,在更新时一切都得到更新,但我只需要更新其中一个

时间:2013-09-11 12:03:14

标签: php mysql sql-update

我知道,我的问题在于我的 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']."' ");
    }
}
?>

很明显,这不起作用,因为其中id ='“。$ row ['id']。”'对于订单中的所有行都为true 表,这就是我整个表不断更新的原因。如何使这项工作在我按下更新按钮的地方只更新行?我希望我足够清楚,谢谢你的帮助!

1 个答案:

答案 0 :(得分: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']."' ");
 }
}

更新记录后使用重定向。