致命错误:未捕获的异常' PDOException' with message' SQLSTATE [HY093]:参数号无效:没有绑定参数

时间:2015-10-13 14:32:37

标签: php mysql database pdo

运行以下代码时出现此错误:

  

致命错误:未捕获的异常' PDOException'与消息   ' SQLSTATE [HY093]:参数号无效:没有参数被绑定'

<?php
include_once("class.user.php");
include('dbconfig.php');
if(isset($_REQUEST["submit"])) {
    if (!empty($_GET["title"]) && !empty($_GET["content"]) && !empty($_GET["category"]) && !empty($_GET["price"])) {
        $sql = "INSERT INTO project VALUES (NULL, :title, :content, :userid, :price, :category);";
        $result = $DB_con->prepare($sql);
        $result->bindValue(":title", $_GET["title"]);
        $result->bindValue(":content", $_GET["content"]);
        $result->bindValue(":userid", $_SESSION["x"]);
        $result->bindValue(":price", $_GET["price"]);
        $result->bindValue(":category", $_GET["category"]);
        $_SESSION['idu'] = $DB_con->lastInsertId();
        $sql_pro='select * from project WHERE title=:title';
        $result_pro=$DB_con->prepare($sql_pro);
        $result_pro->execute();
        $row_pro=$result_pro->fetch(PDO::FETCH_ASSOC);
        if($result_pro->rowCount() >0 ){
            $_SESSION['idu'] = $row_pro['id'];
            return true;
        }

        $sql_upload="INSERT INTO upload VALUES (NULL, :idp , :address);";
        $result_up=$DB_con->prepare($sql_upload);
        $result_up->bindParam(':address',$_SESSION['upload']);
        $result_up->bindParam(':idp',$_SESSION['idu']);
        $result_up->execute();
        header('location:../single-project.php');
        exit;
    } else {
        header("location:../create.php?error=10");
        exit;
    }
}


?>

当删除execute()函数调用时没有错误! 但它没有显示标题或任何内容。如何修复?

<?php
include("controller/check-single-project.php");
include("header.html");
?>
<div class="col-sm-6 col-sm-offset-3" style="margin-top: 50px;">
    <h1 class="text-right"><?php echo $prosingle_r->pro_single('title'); ?></h1>
    <div class="text-right">
        <p class="content-txt"><?php echo $prosingle_r->pro_single('content'); ?></p>
    </div>
</div>

1 个答案:

答案 0 :(得分:3)

erro在这里,你需要再次绑定:title因为是一个不同的查询

改变:

$sql_pro='select * from project WHERE title = :title';
$result_pro=$DB_con->prepare($sql_pro); <-- where is the bind?
$result_pro->execute();

要:

$sql_pro = 'select * from project WHERE title = :title';
$result_pro = $DB_con->prepare($sql_pro);
$result_prod->bindValue(':title', $_GET['title']);
$result_pro->execute();

或者

$result_pro = $DB_con->prepare($sql_pro);
if(!$result_pro->execute(array(':title' => $_GET['title']))){
   print_r($result_pro->errorInfo());
}