I'm saving data from an API, and running a ON DUPLICATE KEY UPDATE
. The thing is that it saved without errors on the first go around, but on consecutive runs it returns false, with the following errorInfo()
Array ( [0] => 00000 [1] => [2] => )
I ran the SQL manually in phpMyAdmin, and it works (0 rows inserted), does this return false when no changes are made?
My apologies for horrible code here, the PDO instance is saved as $this->db
(I'm saving some JSON in this function, so the usual prepare escaping was erroring on the :
, hence the "make-due" solution under)
public function update($table, $valuePairs)
{
$columns = '';
$values = '';
$updates = '';
foreach ($valuePairs as $column => $value)
{
if($value !== null and $value !== '') {
$columns .= '`'.$column.'`, ';
$values .= $this->db->quote($value).', ';
$updates .= '`'.$column.'` = VALUES(`'.$column.'`), ';
}
}
$columns = rtrim($columns, ', ');
$values = rtrim($values, ', ');
$updates = rtrim($updates, ', ');
$sql = 'INSERT INTO '.$table.' ('.$columns.')
VALUES ('.$values.')
ON DUPLICATE KEY UPDATE '.$updates;
$result = $this->db->exec($sql);
if(!$result)
{
print_r($this->db->errorInfo());
echo '<br><br>';
}
return $result;
}