繁体   English   中英

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

[英]Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: no parameters were bound

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

致命错误:未捕获异常'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>

错误就在这里,你需要再次绑定: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());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM