Slevin Slevin - 1 month ago 5
MySQL Question

MySQL update script throws PDOException

Question solved... silly mistake

i have a little problem with my database update script (i'm using PDO and included a PDO class from here)

My logic:

Click on submit button calls "editClient.php" (need this cause i use AJAX):

<?php
// include clients class
require("../classes/clients.class.php");

// get vars & save them in $values array
$id = $_POST['id'];
$id = (int)$id;
$name = $_POST['name'];
$initial = $_POST['initial'];
$payment = $_POST['payment'];
$hourly_rate = $_POST['hourly-rate'];
if ($payment == 'payment-per-service') {
$hourly_rate = "";
}
$active = $_POST['active'];

$values = array($name,$initial,$payment,$hourly_rate,$active,$id);

$client = new Client();
$client->editClient($values);
?>


My client.class.php (shorten):

<?php
// include database class
require_once('../../../lib/php/classes/database/database.class.php');

class Client {

public function __construct() {
$this->db = Database::get("default");
}

public function editClient($values) {
if ($this->db->update("UPDATE clients SET name=?, initial=?, payment=?, hourly_rate=?, active=? WHERE cid=?",$values)) {
} else {
print "Updating failed";
}
}

public function __destruct() {
$this->db = null;
unset($this->db);
}
}
?>


But every time i call my edit client script, i get this error:

Fatal error: Uncaught exception 'PDOException' with message 'PDO-Exception: SQLSTATE[42000]: Syntax error or access violation: 1064 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 ')' at line 1' in /home/www/gopeter/collab/lib/php/classes/database/database.class.php:104

Stack trace:
#0 /home/www/gopeter/collab/lib/php/classes/database/database.class.php(127): Database->_query('UPDATE clients ...', Array, 'update')
#1 /home/www/gopeter/collab/sites/clients/classes/clients.class.php(24): Database->update('UPDATE clients ...', Array)
#2 /home/www/gopeter/collab/sites/clients/ajax/editClient.php(20): Client->editClient(Array) #3 {main} thrown in /home/www/gopeter/collab/lib/php/classes/database/database.class.php on line 104


But my SQL query looks correct?

Answer

You have an error in your SQL syntax near ')'

What about looking at your query near )?

It seems your coding style failed you.

I will never understand a desire to stuff as much operators in one single line as possible.
Look, isn't it much more readable than your gigantic one line?

$sql = "UPDATE clients SET name=?, initial=?, payment=?, hourly_rate=?, active=? 
        WHERE cid=?)";
$res = $this->db->update($sql,$values);
if ($res) {

isn't the wrong brace well spottable here?

Comments