PDO插入不插入

时间:2014-01-07 14:42:12

标签: php mysql pdo

我正在尝试使用一个准备PDO语句插入,但它似乎没有工作我希望它是如何。继承我的代码:

$conn = new PDO('mysql:host=localhost;dbname=myDB', $username, $password);

$sql = "INSERT INTO posts (`text`,`name`) VALUES (:text,:username)";
$q = $conn->prepare($sql);
$q->bindParam(':text', $_GET['textField'], PDO::PARAM_STR);
$q->bindParam(':username', $_SESSION['myusername'], PDO::PARAM_STR);
$q->execute();
print_r($q->errorInfo());

问题是这没有做任何事情,我不知道为什么,我使用遵循指南here来帮助我做到这一点。

编辑:当我使用print_r时,我收到以下MySQL错误:

Array ( [0] => 42000 [1] => 1064 [2] => 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 ''text','name') VALUES ('a','user')' at line 1 ) 

1 个答案:

答案 0 :(得分:1)

因为text是MySQL中的一个类型(look here),所以查询失败,因为语法错误。

在文字名称周围添加标记,然后重试。

$sql = "INSERT INTO posts (text,name) VALUES (:text,:username)";
                           ----
                     This is the error.
相关问题