drxkx6149 2012-09-11 16:45
浏览 56
已采纳

pdo语句无法执行

i have a pdo block for inserting values into my table as follows

try{

        $user = 'root';
        $pass = null;
        $pdo = new PDO('mysql:host=localhost; dbname=divebay', $user, $pass);

        $name = $_POST['name'];
        $desc = $_POST['description'];
        $cond = $_POST['condGroup'];
        $sprice = $_POST['sprice'];
        $iprice = $_POST['iprice'];
        $incprice = $_POST['incprice']; 
        $duration = $_POST['duration'];
        $img = $_POST['img'];


        $owner = $_SESSION['username'];

        $valid = "set";
        $stmt2 = $pdo->prepare("SELECT * FROM auction WHERE ID = :id");
        $stmt2->bindParam(":id", $random, PDO::PARAM_INT);
        while(isset($valid)){
            $random = rand(100000,999999);
            $stmt2->execute();

            if(!$stmt2->fetch(PDO::FETCH_ASSOC)){
                unset($valid);
            }
        }

        $timestamp = time() + ($duration * 24 * 60 * 60);

        $stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description)
                      VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description");
        $stmt->bindParam(':id', $random, PDO::PARAM_INT);
        $stmt->bindParam(':name', $name, PDO::PARAM_STR);
        $stmt->bindParam(':owner', $owner, PDO::PARAM_STR);
        $stmt->bindParam(':holder', $owner, PDO::PARAM_STR);
        $stmt->bindParam(':iprice', $iprice, PDO::PARAM_STR);
        $stmt->bindParam(':sprice', $sprice, PDO::PARAM_STR);
        $stmt->bindParam(':incprice', $incprice, PDO::PARAM_STR);
        $stmt->bindParam(':etime', $timestamp, PDO::PARAM_INT);
        $stmt->bindParam(':img', $img, PDO::PARAM_STR);
        $stmt->bindParam(':condition', $condition, PDO::PARAM_STR);
        $stmt->bindParam(':description', $description, PDO::PARAM_STR);
        if($stmt->execute()){
            $worked ="yes";
        }

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

i cant tell why this statement wont execute, the $worked variable has not been set when it is the script is run. all database column names and datatypes have been checked correct as they are. ive never had a problem with a statement not executing until now. whats wrong? how do i go about debugging this?

  • 写回答

2条回答 默认 最新

  • douyin9987 2012-09-11 17:27
    关注

    If you setup the database connection with error mode exception PDO will throw an exception if something is wrong with your statement. I also see that you are using the MySQL driver for PDO. If you do this you should always disable emulated prepared statements. So I would write you connection as following (note that I have also set the encoding):

    $pdo = new PDO('mysql:host=localhost; dbname=divebay;charset=utf8', $user, $pass);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    

    Also see this post for more information about this.

    Once you have done this you will see that your statement is wrong. You have one missing ) at the end of the statement:

    $stmt = $pdo->prepare("INSERT INTO auction(ID, name, owner, holder, sprice, iprice, incprice, etime, img, condition, description)
                           VALUES (:id, :name, :owner, :holder, :sprice, :iprice, :incprice:, :etime, :img, :condition, :description)");
                                                                                                                                    ^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路