dpoxk64080 2011-04-10 11:02
浏览 58
已采纳

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.

  • 写回答

3条回答 默认 最新

  • dsjmrpym220113739 2011-04-10 11:48
    关注

    Don't use dashes in your SQL statements.

    Make this:

    :user-id
    

    as this:

    :userid
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入