How to debug the concatenation of a SQL query in PHP? -


there's query, cannot manage find what.

    $keys = array_keys($fields);     $values = array_values($fields);      $sql = "update " .$table. " set " .implode("`, `", $keys) ."='".implode("', '", $values) . "' id={$id}"; 

and shows : update users set name, password'rick vets', 'sdfg' id=5

but has show : update users set name = 'rick vets', password='sdfg' id=5

try looping through $fields array create update string this:

$update_string='';  foreach ($fields $key=>$value) {     $update_string .= $key."='$value', "; } 

then remove last comma character string using rtrim() function:

$update_string = rtrim($update_string, ", "); 

then query becomes:

$sql = "update " .$table. " set " .$update_string. " id={$id}"; 

this illustrate concept since code might still open sql injection attacks, in case should use prepared statement.


Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -