简体   繁体   中英

MYSQL update, replace delete with PHP array

I have a query I need to run on a $_POST array with php it's quite simple as it takes all the values in the array then runs a query for each of the values which updates the database, here is the tricky part: As the array is dependent of checkboxes selected by the user the query has to either Update, Replace or remove values in DB dependent on what is in the $_POST array. I've only been able to have the query successfully run the last value in the array.
I've tried both 'REPLACE INTO' and 'INSERT IGNORE INTO' but with no luck. I'm starting to think much more complex coding is going to be needed :( here's what I have so far:

foreach($params['text'] as $boilertext)
{
    $sql = 'UPDATE '._DB_PREFIX_.'boilertexts 
            SET id_product="'.pSQL($boilertext).'" WHERE id_text="'.$params['id'].'"';
    if(!Db::getInstance()->Execute($sql))
    {
        echo mysql_error();
    }
}

EDIT:

This is the post params i'm getting:

Array ( 
       [text] => Array ( [0] => 8 [1] => 6 [2] => 10 ) // the Text ID i'm updating
       [textValue] => "Some text that I'm adding to another DB table"
       [id] => 28 // The ID that all the params from ['text'] are linked too
       [update] => update // Name of the submit button I'm using for form processing
     ) 

My issue is that although the foreach is running the SQL x amount of times for each query (tested it by printing out a count for each query) it will only insert the last value in ['text'] array. It also doesn't seem to update/remove the text id from the DB if it is not in the array

What you really do is 3 updates on same record . Thats why you get only last value in database.

You are updating record id_text=28 with values 8,6,10.

It will only end up set as the last value, as you are updating the same record the number of times that you have values in your array.

Only one field in one record is being processed, it can not magically become multiple records this way - you would need a separate table, or use INSERT .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM