0

how to update table in foreach loop with where clause

my code:

     foreach($output as $var) {
     $tmp = explode(",", $var);  
     $collec_date = $tmp[2];
     $cust_id = $tmp[4];     
     $cust_name = $tmp[5];       
     $install_no = $tmp[6];
     $amount = $tmp[7]; 

 $sql = "UPDATE ankali_slabpay  SET install_date='$collec_date', install_amt='$amount' WHERE ac_no='$cust_id' AND install_no='$install_no' AND $install_amt='000000' GROUP BY ac_no";  
$result = mysql_query($sql);  
}   
4
Contributors
5
Replies
21
Views
3 Years
Discussion Span
Last Post by Atli
0

What is wrong with this code? What is happening that shouldn't be? What isn't happening that should be? Are you getting any errors? - We need details.

There is one thing in there that should be fixed, if you want any relevant debug information. That is: you seem to be ignoring the result of the UPDATE query. You should be checking if it fails and print/log the error.

$result = mysql_query($sql);
if (!$result) {
    trigger_error("UPDATE query failed: " . mysql_error(), E_USER_ERROR);
}

Whether or not this is printed or logged depends on your server settings. Be sure to look into how that works!

0

$cust_id = $tmp[4]; will always be at position 4.

What is the exact problem you're having?

1

pritaeas is right. The UPDATE command doesn't use GROUP BY clauses. It shouldn't be there. - I'm guessing this UDPATE statement you are using was created based on a SELECT statement, and you aren't 100% sure how that should be done?

One other oddity in this UPDATE statement is the last condition in the WHERE clause: AND $install_amt='000000'. Where is the $install_amt variable comming from? Why are you using a PHP variable as a field name? Is it perhaps not meant to be a PHP variable, and the $ is in there by accident? - Also, why is it supposed to be equal to a string of six zero chars?

If you go back to my first post in this thread, and implement the error handling I suggested, we wouldn't have to be guessing like this. PHP would tell you exactly what is going wrong.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.