UPDATE values in a loop

Hello everyone.
I need to update a value random generated(‘hash’) in some rows, only if the previous value was 0 and if another field is equal to the var $event. Everything works, but if I refresh the page, it overwrites one row of the loop. I wouldn’t expect that because there are no more rows with hash field equal to 0 ( I can see that in the phomyadmin ). It keeps on doing that as many times as the number of rows generated by the query are. I’m sure the problem is in the loop, but I can’t figure it out. Here the code:

$event= $_POST['event'];
$sql = "SELECT * FROM digital_orders where hash=0 and event='$event'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        $key = uniqid(md5(rand()));
        $id= $row["ID"];
    $registerid = "UPDATE digital_orders SET hash='$key' where id='$id' " ;
    $go = $conn->query($registerid) ;

Look at the values of num_rows and $row, ther must be something in it. var_dump()

Ty. The problem was with the type of variable and operators. ‘Hash’ is a string, not a number.

hash =‘0’

It did the trick

You might want to have a look at prepared statements, as that can get rid of a lot of quoting issues, among other advantages.

Ty, I will

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.