【问题标题】:MySQL update script throws PDOException [closed]MySQL更新脚本抛出PDOException [关闭]
【发布时间】:2012-03-18 08:03:59
【问题描述】:

问题解决了……愚蠢的错误

我的数据库更新脚本有点问题(我正在使用 PDO 并包含 PDO class from here

我的逻辑:

点击提交按钮调用“editClient.php”(需要这个因为我使用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);
?>

我的client.class.php(缩短):

<?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);
    }   
}
?>

但每次我调用我的编辑客户端脚本时,我都会收到此错误:

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

但是我的 SQL 查询看起来正确吗?

【问题讨论】:

  • 顺便说一句:这使您的代码无法测试:$this-&gt;db = Database::get("default");
  • 好的,你能告诉我为什么吗?
  • 你只有tightly coupled Client 类和Database 类。
  • 当你在它的同时也搜索其他干净的代码谈话。 :-)

标签: php mysql database pdo


【解决方案1】:

您的 SQL 语法在 ')'附近有错误

看看)附近的查询怎么样?

看来你的编码风格让你失望了。

我将永远不会理解将尽可能多的运算符塞入一行的愿望。
看,它是不是比你那巨大的一行更具可读性?

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

这里不是很容易发现错误的大括号吗?

【讨论】:

  • 我肯定看过 ) 附近的 SQL 语法......但我没有看到任何失败?!
  • 真的吗?应该有任何)s吗?它的开口在哪里(那么?
  • 哦hhhhhhhh该死的......对不起愚蠢的问题,我真是个白痴......
猜你喜欢
  • 2015-09-20
  • 2015-04-12
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多