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 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line