crashtest
crashtest

Reputation: 99

MySql query returns multiple results, on update everything gets updated, but I only need one of them updated

I know, that my problem lies in that in my where statement there is no unique identifier which row should be updated only. Here is what I have just to make it more clear:

<?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']."' ");
    }
}
?>

It is obvious that this doesn't work, because where id='".$row['id']."' is true for all of the rows in the orders table and this is why my whole table gets constantly updated. How do I make this work so that only the row gets updated where I have pressed the Update button? I hope I was clear enough, thanks for heliping!

Upvotes: 1

Views: 101

Answers (1)

Rajeev Ranjan
Rajeev Ranjan

Reputation: 4142

do it with hidden field

$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']."' ");
 }
}

use redirect after updating record.

Upvotes: 1

Related Questions