du16178 2017-04-24 15:58
浏览 39
已采纳

PDO INSERT查询500服务器错误[重复]

This question already has an answer here:

I am new to using PDO and I am getting an http 500 server error when the form is submitted.

The php page with the processing code is in the correct folder so I don't know why its throwing up a 500 error.

There is NO url rewrite going on neither .

Here is my code:

        try {
        $dbh = new PDO("mysql:host=$hostname;dbname=crm",$username,$password);

        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line

        $stmt = $db->prepare("INSERT INTO testdrives (forename, surname,phone,email,add1,add2,add3,city,county,postcode,car,date) VALUES (:forename, :surname,:phone,:email,:add1,:add2,:add3,:city,:county,:postcode,:car,:date)");
                $stmt->bindParam(':forename', $_POST['forename']);
                $stmt->bindParam(':surname', $_POST['surname']);
                $stmt->bindParam(':phone', $_POST['phone']);
                $stmt->bindParam(':email', $_POST['email']);
                $stmt->bindParam(':add1', $_POST['add1']);
                $stmt->bindParam(':add2', $_POST['add2']);
                $stmt->bindParam(':add3', $_POST['add3']);
                $stmt->bindParam(':city', $_POST['city']);
                $stmt->bindParam(':county', $_POST['county']);
                $stmt->bindParam(':postcode', $_POST['postcode']);
                $stmt->bindParam(':car', $_POST['car']);
                $stmt->bindParam(':date', $_POST['date']);
                $stmt->execute();
        if ($dbh->query($sql)) {
        echo "<script type= 'text/javascript'>alert('New Record Inserted Successfully');</script>";
        }
        else{
        echo "<script type= 'text/javascript'>alert('Data not successfully Inserted.');</script>";
        }

        $dbh = null;
        }
        catch(PDOException $e)
        {
        echo $e->getMessage();
        }
</div>
  • 写回答

2条回答 默认 最新

  • dsf6778 2017-04-24 16:11
    关注

    Edit:

    I missed the incorrect variable used for the connection being $dbh and $db in the prepare; my bad.


    Original answer.

    This line:

    if ($dbh->query($sql)) {...}
    

    is failing for two reasons:

    • Calling query() on what is already being prepared/executed.
    • Using a non-existant variable, $sql.

    Get rid of that statement with the related brace and replace it with simply and replacing the $stmt->execute(); with:

    if($stmt->execute()){
    
            // success
    
        } else{
    
            // error
    
        }
    

    and using PDO's error handling (as you are doing now) and PHP's error reporting:

    Check your logs also.

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?