tyler_durden Posted October 18, 2010 Share Posted October 18, 2010 $new_array2=array_diff($my_array,$itemIds); $updatedb = mysql_query("UPDATE content_type_ads SET field_expire_value = '666' WHERE $new_array2 = field_item_id_value"); "$new_array2" has only 12 digit numbers for each item in the array, and I want to match them to the "field_item_id_value" field in the table, updating the "field_expire_value" field for the record where they match. I know I am close because the UPDATE code works before I add the WHERE statement (it of course adds '666' to EVERY field, but for a noobie it's a start!). Quote Link to comment https://forums.phpfreaks.com/topic/216181-trying-to-update-a-db-field-where-another-field-and-an-array-item-match/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 18, 2010 Share Posted October 18, 2010 If $new_array2 is a php array, be advised that you cannot directly put it into a query as there is no mysql array data type or even mysql array functions. Your best bet would be to form a comma separated list and use the mysql IN() function. Quote Link to comment https://forums.phpfreaks.com/topic/216181-trying-to-update-a-db-field-where-another-field-and-an-array-item-match/#findComment-1123536 Share on other sites More sharing options...
tyler_durden Posted October 18, 2010 Author Share Posted October 18, 2010 Thanks for the suggestions. I researched the IN function a bit and implemented the below code. I am now getting an error "Parse error: syntax error, unexpected ';' in " on that line, and if I remove the ";" then the error moves to the next line where I have "?>", both unexpected ends. Do I have to create the comma seperated list BEFORE this step? $updatedb = mysql_query("UPDATE content_type_ads SET field_expire_value = '666' WHERE field_item_id_value IN (".implode(',', $new_array2).")"; Quote Link to comment https://forums.phpfreaks.com/topic/216181-trying-to-update-a-db-field-where-another-field-and-an-array-item-match/#findComment-1123551 Share on other sites More sharing options...
tyler_durden Posted October 18, 2010 Author Share Posted October 18, 2010 Alright, got rid of the errors with the code below, but it is still not updating the correct db fields/records. $updatedb = mysql_query("UPDATE content_type_ads SET field_expire_value = '666' WHERE field_item_id_value IN (".implode(',', $new_array2).""); Quote Link to comment https://forums.phpfreaks.com/topic/216181-trying-to-update-a-db-field-where-another-field-and-an-array-item-match/#findComment-1123563 Share on other sites More sharing options...
tyler_durden Posted October 18, 2010 Author Share Posted October 18, 2010 Woohoo! Thanks for pointing me in the right direction. The below code worked! $ImplodedArray = implode(",",$new_array2); $updatedb = mysql_query("UPDATE content_type_ads SET field_expire_value = '666' WHERE field_item_id_value IN ($ImplodedArray)"); Quote Link to comment https://forums.phpfreaks.com/topic/216181-trying-to-update-a-db-field-where-another-field-and-an-array-item-match/#findComment-1123573 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.