premier213
premier213

Reputation: 119

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

I get this error when I run the code below:

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

<?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;
    }
}


?>

When remove execute() function call there is no error ! but it does not show title or any content .How can this be fixed?

<?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>

Upvotes: 1

Views: 1070

Answers (1)

rray
rray

Reputation: 2556

The erro is here, you need bind again :title because is a diferente query

change :

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

To:

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

Or

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

Upvotes: 3

Related Questions