PDO 未插入数据库

发布于 2024-10-31 19:59:05 字数 980 浏览 1 评论 0原文

希望有人能够帮助我,因为我整个晚上都在用头撞墙试图解决这个小问题。

我想使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぽ尐不点ル 2024-11-07 19:59:05

不要在 SQL 语句中使用破折号。

做这个:

:user-id

像这样:

:userid

Don't use dashes in your SQL statements.

Make this:

:user-id

as this:

:userid
年华零落成诗 2024-11-07 19:59:05

$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 defining PDO::PARAM_INT. Try replacing it with PDO::PARAM_STRING or try setting $userId to a number(1 for example)

╰ゝ天使的微笑 2024-11-07 19:59:05

如果出现问题,那么肯定是出现了错误。首先,您需要通过检查返回值来找出哪个语句失败(始终首先阅读 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文