dourong9253 2018-07-17 17:22
浏览 222

当我尝试使用PHP变量查询时,PDO查询返回bool(false)

I'm trying to make a query with a PHP variable ($key) from $_POST form data, and I used bindParam to bind the parameter to the variable.

    foreach ($_POST as $key => $value) {
      if (isset($value) && is_numeric($value)) {
        $sql = "SELECT productPrice FROM products WHERE productName=:productName";
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':productName', $key, PDO::PARAM_STR);
        $stmt->execute();

        $result = $stmt->fetch(PDO::FETCH_ASSOC);
        var_dump($result);
    }

This returns 'bool(false)' on my page, and I'm not sure why it's doing that. I made sure the $key variable accurately matches the data on the SQL table that I'm querying for.

  • 写回答

1条回答 默认 最新

  • doulu4976 2018-07-17 17:48
    关注

    You are assigning your $key to $value

    foreach ($_POST as $key => $value) {

    And after this you are still using $key instead of $value in this line

    $stmt->bindParam(':productName', $key, PDO::PARAM_STR);

    Try using this,

    $stmt->bindParam(':productName', $value, PDO::PARAM_STR);

    I hope it'll help you.

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程