简体   繁体   中英

Prevent MySQL from updating column with empty string?

Found this answer helpful , however I have about 100 inputs that need updating.

I currently have this code for cleaning up the values:

foreach($_POST as $key => $value) {
$data[$key] = filter($value);}

Can I add the answer from the above link to this code to affect all 100 inputs or would I be forced to add the IF clause on each input.

Thank you.

$update_arr = array();
foreach ($_POST as $key => $value)
{
    if (strlen($value) !== 0)
    {
        $update_arr[] = $key.' = "'.filter($value).'"';
    }
}
$update_string = implode(',',$update_arr);
$table = '';
$where = '';

$format = "UPDATE %s SET %s WHERE %s";
$sql = sprintf($format,$table,$update_string,$where);

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