1
I have a Dbupdate function that updates the data in the database. This function creates a query based on the array passed as parameter.
For example, if I send this array (like $visit
)
'ips' => '192.168.25.1, 127.0.0.1'
'real' => 1
'last' => '2017-05-19 18:15:50'
'desktop' => 10
When executing DBExecute('clientes', $visit, 'country = pt_BR')
this query is returned to me:
UPDATE ac_analytics SET ips = '192.168.25.1, 127.0.0.1', real = '1', last = '2017-05-19 18:15:50', desktop = '10' WHERE country = 'pt_BR'
And then I get sql error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'real = '1', last = '2017-05-19 18:15:50', desktop = '10' WHERE country = 'pt_BR'' at line 1
My full update function:
//Altera registros
function DBUpdate($table, array $data, $where = null){
foreach ($data as $key => $value) {
$fields[] = "{$key} = '{$value}'";
}
$fields = implode(', ', $fields);
$table = DB_PREFIX.'_'.$table;
$where = ($where) ? " WHERE {$where}" : null;
$query = "UPDATE {$table} SET {$fields}{$where}";
return DBExecute($query); // Função de execução de queries
}
@Knautiluz Exactly what I needed. I will change the name of the tables... Thank you.
– Daniel Bonifácio
You do not need to change the name of the tablets. Use the escape character (crase). Example:
ips
,ac_analytics
,last
and so on. It’s always good to use it because Mysql and other SGDB have many reserved words and time or other you will run into them.– Clayderson Ferreira