php – SQLSTATE [HY093]:插入mysql db期间的pdo语句

前端之家收集整理的这篇文章主要介绍了php – SQLSTATE [HY093]:插入mysql db期间的pdo语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的 PHP代码实现插入到db:
<?PHP

require_once "includes/db_data_inc.PHP";

try 
{
    $DBH = new PDO("MysqL:host=$db_host;dbname=$db_name",$db_user,$db_pass);

    $DBH->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

    $cathy = new patient($_POST['name'],$_POST['surname'],$_POST['address'],$_POST['birth-place'],$_POST['province'],$_POST['dt'],$_POST['gender'],$_POST['select']);

    $STH = $DBH->prepare("INSERT INTO users (name,surname,address,birth_place,province,dt,sex,case) value (:name
                                                        :surname,:address,:birth_place,:province,:dt,:sex,:case)");

    $STH->execute((array)$cathy);

}
catch (PDOException $pdoe) 
{
    error_log($pdoe->getMessage());
    die("An error was encountered!");
}

?>

这里是db_data_inc.PHP,其中存储了db_info以及我创建对象患者的位置

$db_host = 'localhost';

$db_name = 'main_db';

$db_user = 'root';

$db_pass = 'root';

/* Create an object patient */

class patient
{
    public $name;
    public $surname;
    public $address;
    public $birth_place;
    public $province;
    public $birth_date;
    public $sex;
    public $case;

    function __construct($nm,$sur,$addr,$bp,$pr,$bd,$sx,$cs)
    {
        $this->name = $nm;
        $this->surname = $sur;
        $this->address = $addr;
        $this->birth_place = $bp;
        $this->province = $pr;
        $this->birth_date = $bd;
        $this->sex = $sx;
        $this->case = $cs;
    }

}

我收到此错误

[10-Feb-2012 21:14:29] sqlSTATE[HY093]: Invalid parameter number: parameter was not defined

但我没有意识到原因……为什么我得到这个错误?有人可以帮帮我吗?哪里出错了?

在您的查询中,您使用:dt作为占位符,但在类构造函数中,您使用$this-> birth_date.

一旦转换,这将创建一个索引为’birth_date’的数组,该数组与命名参数“dt”不匹配:选择一个或另一个.

原文链接:https://www.f2er.com/php/138490.html

猜你在找的PHP相关文章