duannue2455 2013-12-23 23:22
浏览 27

PDO插入查询不起作用[重复]

This question already has an answer here:

I'm rebuilding my current script to support PDO instead of MySQL queries. I am stuck on this insert query though, it's not executing and I have no clue why.

Google'd around a bit but couldn't find anything.

                try 
                {
                    $sql = "INSERT INTO
                                news (news_name,
                                    news_description,
                                    news_author,
                                    news_date,
                                    news_link,
                                    news_category)
                            VALUES ('" . trim($_POST['news_name']) . "',
                                '" . trim($_POST['news_description']) . "',
                                " . $_SESSION['admin_id'] . ",
                                NOW(),
                                '" . trim($_POST['news_link']) . "',
                                '" . trim($_POST['news_category']) . "'
                                )";
                    $results = $db->exec($sql);
                    $id = $db->lastInsertId();

                    if($results)
                    {
                        echo $id;
                        echo '<p>News item added succesfully</p>';
                        echo '<a href="admin.php">Click here to return to the admin panel</a>';                     
                    }
                } 
                catch(PDOException $e)
                {
                    echo $e->getMessage();
                }
            } 
            ?>
</div>
  • 写回答

1条回答 默认 最新

  • duankuang1046 2013-12-23 23:41
    关注

    First, set PDO to throw exceptions when it encounters an error if you haven't already...

    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    Now, prepare an INSERT statement with parameters

    $sql = <<<_SQL
    INSERT INTO `news` (news_name, news_description, news_author,
                        news_date, news_link, news_category)
    VALUES (:name, :description, :author_id, NOW(), :link, :category)
    _SQL;
    $stmt = $db->prepare($sql);
    

    Then execute it with your values

    $stmt->execute([ // PHP 5.4 short array syntax, use array(...) if not available
        ':name'        => trim($_POST['news_name']),
        ':description' => trim($_POST['news_description']),
        ':author_id'   => $_SESSION['admin_id'],
        ':link'        => trim($_POST['news_link']),
        ':category'    => trim($_POST['news_category'])
    ]);
    

    And finally...

    catch(PDOException $e)
    {
        echo $e->getMessage();        
    }
    

    Never do this. Not only does it expose internal information to end users but it lets your program continue on as if nothing happened.

    When developing, let the exception go unhandled, thus terminating execution. In production, implement a high-level exception handler that can log and / or notify you of the problem while presenting users with a friendly error message.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用