I have an array with different IDs which I get from my database.
$fotos_temp_list=explode(",",$fotos_temp["fa_id"][0]);
Result: fa_id => 15,16,17,18
Now I want to update all rows inside another table with the IDs 15,16,17,18 and insert a specific '$id'
into a column called 'fa_ev_id'
. In this example I set:
$id=1;
Now my foreach-loop looks like this:
foreach ($fotos_temp_list as $key) {
UPDATE images SET fa_ev_id = Concat(fa_ev_id , ',' ,'".$id."') where fa_id='".$key."' ";
}
This part is working too. Every row with my IDs (in my example: 15,16,17,18) gets updated.
Problem: When I run the foreach-loop again, the '$id'
will be saved again inside the row. See here:
Question: How is it possible, that I can check if '$id'
is already inside the row and if so, it gets skipped? Here is what I tried to do:
foreach ($fotos_temp_list as $key) {
if (!in_array($id,$key)===true) {
UPDATE fotoalbum SET fa_ev_id = Concat(fa_ev_id , ',' ,'".$id."') where fa_id='".$key."'
}
}
I think the in_array
function checks if '$id'
id already inside '$key'
and if it is true, the UPDATE statement is done. Therefore I added '!in_array'
but it doesn´t seems to work. Does anyone has an idea what I am doing wrong? I just want to check if the '$id'
is already inside my database row and if so, it should not insert the '$id'
again. I appreciate any advice.
The in_array() function in php accepts an array as its second parameter, in (!in_array($id,$key)===true) , https://www.w3schools.com/php/func_array_in_array.asp . The second parameter $key , passed in here isn't an array.
You could save the datatype of fa_ev_id as json, and retrieve and json_decode the value when you are about performing an update the field to know if that $id already exists using in_array() or you could retrieve the values, expolode them to form an array and then check if it exists with in_array() .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.