PHP和SQLSTate错误

时间:2015-11-27 14:54:46

标签: php sql

我有这个PHP代码:

<?php
require_once('../include/inner_global.php');
$id=$_REQUEST['id'];
$hostdb="localhost";
$namedb="architect";
$userdb="root";
$passdb="root";
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8");
if(isset($_POST['submitDate'])){
if(!isset($_POST['Date'])){
    echo "No date selected";    
}else{
echo $d = date('Y-m-d',strtotime($_POST['Date']));
}


                    //foreach($result as $row){
    $sql1= "SELECT SUM(total_pay) AS total FROM workers WHERE date_of_pay = :d AND projects_id = :id";
    $stmt = $conn->prepare($sql1);

    $stmt->bindValue(":d", $d);
    $stmt->bindValue(":projid", $id);
    $count = $stmt->execute();
    $result = $stmt->fetchAll();
    echo var_dump($result);
}
?>

我收到了这个错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in C:\wamp\www\architect\pages\projDReport.php on line 25

有人知道出了什么问题吗? global.php文件用于会话检测,否则它将带我们登录页面。

1 个答案:

答案 0 :(得分:2)

$stmt->bindValue(":projid", $id);更改为$stmt->bindValue(":id", $id);

您在预准备语句中绑定的值应具有与语句中定义的相同的名称。在这种情况下,它应该命名为:id而不是:projid