2015-10-13 77 views
1

当我运行下面的代码我得到这个错误:致命错误:未捕获的异常“PDOException”与消息“SQLSTATE [HY093]:无效参数号:没有参数被束缚

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


?> 

当删除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> 
+0

检查错误 –

+2

另外,这个问题看起来像http://stackoverflow.com/q/33087077/ –

+0

的转贴在哪个查询中你会得到错误?第一次插入?选择?另一个插入?并且对于测试 – rray

回答

3

的埃罗在这里,你需要重新绑定:title因为是diferente查询

变化:

$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()); 
} 
相关问题