I have a multidimensional array att I want to insert in MySQL through an UPDATE statement. But I only want it to be updated if the entry_id from the array fits with entry_id from DB. If it matches the values from the array, it has to be updated.
This is my multidimensional array named values
Array (
[0] => Array ( [entry_id] => 41149 [o_number] => 000001 [test1] => 000001 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[1] => Array ( [entry_id] => 41142 [o_number] => 000202[test1] => 000202 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[2] => Array ( [entry_id] => 41103 [o_number] => 000003 [test1] => 000003 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 )
[3] => Array ( [entry_id] => 41101 [o_number] => 000044 [test1] => 000044 [test2] => 1234 [lev] => Ja [fak] => Manuel/brev [beta] => 10 [test] => 2 )
[4] => Array ( [entry_id] => 41100 [o_number] => 000542 [test1] => 000542 [test2] => 1234 [lev] => Ja [fak] => Mail [beta] => 30 [test] => 4 ))
This is my DB fields, i want to update.
o_number test1 test2 lev fak beta
And this is how my DB looks like now
title entry_id o_number test1 test2 lev fak beta
Rest Soya 41149
Cafe Bella 41142
Danglette 41103
This is my code, for how to iterate throug an dimensional array and then update mySQL. But i only want to update, if the entry_id matches.
So the question is, how can I update the table when I have a multidimensional array? I tried it this way, but have not decided to test it because I do not want to insert anything wrong. Is there a better and more efficient way to do this?
foreach ($values as $key)
{
$sql_update = "UPDATE exp_channel_data set
o_number =$key['o_number'],
lev ='$key['lev']',
fak =$key['fak'],
beta =$key['beta'],
test1 =$key['test1'],
test2 =$key['test2']
where entry_id = '$entry_id"
$this->EE->db->query($sql_update);
}
This is my desired output in my DB
title entry_id o_number test1 test2 lev fak beta
Rest Soya 41149 000001 000001 1234 Ja Mail 30
Cafe Bella 41142 000202 000202 1234 Ja Mail 30
Danglette 41103 000003 000003 1234 Ja Mail 30