Jump to content

Trying to update a DB field where another field and an array item match


tyler_durden

Recommended Posts

$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!).

 

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.

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).")";

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)."");

 

 

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)");

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.