Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

Update Data Multiple Columns MySql Database Table PHP Function

//Update Data Into Multiple Columns
function updateAllColumns($table, $where = '', ...$params): bool
{
    global $db;
    $sql = "UPDATE $table SET ";
    foreach ($params[0] as $key => $val) {
        if ($val != 'NULL') {
            $sql .= "`{$key}`=" . "'{$val}' ,";
        } else {
            $sql .= "`{$key}`=" . ' NULL' . ",";
        }
    }
    if ($where) {
        $sql .= ' WHERE ' . $where . ' ';
    }
    $sql = substr_replace($sql, '', strrpos($sql, ','), 1);
    $result = $db->query($sql);

    if ($result) {
        return true;
    } else {
        return false;
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Update #Data #Multiple #Columns #MySql #Database #Table #PHP #Function
ADD COMMENT
Topic
Name
3+4 =