PDO 未插入数据库
希望有人能够帮助我,因为我整个晚上都在用头撞墙试图解决这个小问题。
我想使用 PDO 将数据插入数据库(我承认对此不是最了解的)。我正在使用过去多次使用过的语句,但由于某种原因,这次它不起作用。语句如下:
$userID = "Johnny5";
$sql = "INSERT INTO user_info(user_id) VALUES(:user-id)";
if($stmt = $this->_db->prepare($sql))
{
$stmt->bindParam(":user-id", $userID, PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
return TRUE;
} else {
return FALSE;
}
但不幸的是,这总是返回 TRUE
,而没有在我的数据库中输入任何内容。我已经尝试了所有我能想到的对声明的组合更改,但我仍然不知所措。
我希望有人能指出我犯的一个非常简单的错误。
另外,在 $sql
字符串中的参数 :user-id
周围放置单引号是我可以让任何内容出现在数据库中的唯一方法,但这显然不向数据库输入任何实际数据。
编辑 我还将 PDO 参数类型从 PDO::PARAM_STR
更改为 PDO::PARAM_INT
但仍然没有运气。
经过进一步调查,execute()
返回 FALSE
。
解决方案 感谢大家的指导。 @Nabeel 说不要在 PDO 参数中使用占位符是正确的。
Hopefully someone will be able to help me because I've been banging my head against the wall all night trying to solve this little problem.
I want to insert data into a database using PDO (which I am admittedly not the most knowledgeable about). I am using a statement that I have used many times in the past, but for some reason this time it's not working. The statement is as follows:
$userID = "Johnny5";
$sql = "INSERT INTO user_info(user_id) VALUES(:user-id)";
if($stmt = $this->_db->prepare($sql))
{
$stmt->bindParam(":user-id", $userID, PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
return TRUE;
} else {
return FALSE;
}
But unfortunately this is always returning TRUE
without ever entering anything into my database. I have tried every combination changes to the statement that I could think of, but I am still at a loss.
I hope someone out there can point out a really simple error that I have made.
Also, placing single quote marks around the parameter :user-id
in the $sql
string is the only way that I can get anything to appear into the database, but that obviously doesn't enter in any actual data into the database.
EDIT
I have also changed the PDO parameter types from PDO::PARAM_STR
to PDO::PARAM_INT
but have still had no luck.
After further investigation, execute()
is returning FALSE
.
Solution
Thanks to everyone for their guidance. @Nabeel was correct in saying not to use placeholders in PDO parameters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要在 SQL 语句中使用破折号。
做这个:
像这样:
Don't use dashes in your SQL statements.
Make this:
as this:
$userId
在此代码中是一个字符串,但您通过定义PDO::PARAM_INT
说它是 int 形式。尝试将其替换为PDO::PARAM_STRING
或尝试将$userId
设置为数字(例如 1)$userId
is a string in this code, yet you say it's in int by definingPDO::PARAM_INT
. Try replacing it withPDO::PARAM_STRING
or try setting$userId
to a number(1 for example)如果出现问题,那么肯定是出现了错误。首先,您需要通过检查返回值来找出哪个语句失败(始终首先阅读 PHP 手册中您使用的每个命令)。
如果您找到了失败的命令,请使用 PDOStatement->errorInfo 以了解有关具体错误的更多信息。
该信息应该可以帮助您解决问题。
更新:如果您想处理异常(如 Pekka 建议):如何从 PDO 中挤出错误消息?
If something fails, then there most certainly was an error. First you need to find out which statement fails by checking their return values (always read about every command you use in the PHP Manual first).
If you've located which command failed, use PDOStatement->errorInfo to find out more about the concrete error.
That information should help you to solve your issue.
Update: And if you want to deal with Exceptions (as Pekka suggested): How to squeeze error message out of PDO?