【问题标题】:MySql query returns multiple results, on update everything gets updated, but I only need one of them updatedMySql 查询返回多个结果,更新时一切都会更新,但我只需要更新其中一个
【发布时间】:2013-09-11 12:03:14
【问题描述】:

我知道,我的问题在于我的 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 中的所有行都是 true 表,这就是为什么我的整个表会不断更新。我该如何进行这项工作,以便仅在我按下 Update 按钮的地方更新行?我希望我已经足够清楚了,谢谢你的帮助!

【问题讨论】:

  • 每对提交项目的隐藏行,id在哪里传递?然后取这个值
  • 在接受订单 ID 的页面上有一个表单怎么样?

标签: php mysql sql-update


【解决方案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']."' ");
 }
}

更新记录后使用重定向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多