PDO:参数号无效:未定义参数

时间:2015-01-21 17:28:41

标签: php mysql pdo

作为一个pdo类,我使用https://github.com/indieteq/PHP-MySQL-PDO-Database-Class这是我的插入代码。插入操作时出现错误(选择函数可以解决问题)我不知道如何解决它,感谢您的帮助。

$insert = $db->query("INSERT INTO post (user_id,post_name,color,start_date,expire_date,content,title,status,rate,category,created,changed,target) VALUES (:user_id,:post_name,:color,:start_date,:expire_date,:content,:title,:status,:rate,:category,:created,:changed,:target)", array( 
                ':user_id' => $this->user_id,
                ':post_name' => $this->post_name(),
                ':color' => $params['color'],
                ':start_date' => $params['start_date'],
                ':expire_date' => $expDate,
                ':content' => $params['content'],
                ':title' => $params['title'],
                ':status' => $params['status'],
                ':rate' => $params['rate'],
                ':category' => $params['category'],
                ':created' => $cDate,
                ':changed' => $chang,
                ':target'=> 1
            ));

Unhandled Exception. 
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
You can find the error back in the log.

Time : 18:19:05
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
Raw SQL : INSERT INTO post (user_id,post_name,color,start_date,expire_date,content,title,status,rate,category,created,changed,target) VALUES (:user_id,:post_name,:color,:start_date,:expire_date,:content,:title,:status,:rate,:category,:created,:changed,:target)

这是我的vardump表单$ _POST表格传递

array(9) {
  ["color"]=>
  string(6) "c22bc2"
  ["start_date"]=>
  string(19) "2014-12-31 18:23:31"
  ["expire_date"]=>
  string(19) "2015-01-08 18:23:33"
  ["content"]=>
  string(70) "
sDOROS)()GJ)*(EH)* VN#MT#:????><?<>?>??)T#\\\'\\

"
  ["title"]=>
  string(23) "Tesssssssssssssssssssss"
  ["status"]=>
  string(7) "publish"
  ["target"]=>
  string(4) "stud"
  ["rate"]=>
  string(1) "5"
  ["category"]=>
  string(5) "w_zal"
}

如果需要

$cDate = date("Y-m-d H:i:s");
$chang = 0;
    public function post_name()
    {
        global $db;
        $n      = $db->row("SELECT max(id) as id FROM post");
        $post_name = $this->user . "-" . $n['id'] . "-" . session_id();

        return $post_name;
    }

    private function user_id()
    {
        global $db;
        $userid        = $db->row("SELECT * FROM users WHERE user_name = :name", array(
            "name" => $_SESSION['user_name']
        ));
        $this->user_id = $userid['user_id'];
        return $this->user_id;

    }

全班:http://pastebin.com/ehPazdMm

1 个答案:

答案 0 :(得分:2)

此数据库类已在内部添加冒号。在readme示例中给出了它们表明您不必在您要绑定的数组的键中放置冒号的示例。

你也可以在the class的第142行看到这个,函数->bind()

public function bind($para, $value)
{   
    $this->parameters[sizeof($this->parameters)] = ":" . $para . "\x7F" . utf8_encode($value);
}