SQLSTATE [HY093]:在插入mysql db期间的pdo语句 [英] SQLSTATE[HY093] : pdo statement during insert into mysql db

查看:115
本文介绍了SQLSTATE [HY093]:在插入mysql db期间的pdo语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是实现插入db的我的php代码:

Here it is my php code to realize insert into 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的位置以及我创建对象Patient的位置

Here it is db_data_inc.php where are stored db_info and where I create the object patient

    $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

但是我不知道原因...为什么我收到此错误?有人可以帮助我吗?错误在哪里?

But I didn't realize the reason...why I got this error? someone can help me? Where is the mistake?

推荐答案

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

In your query you use :dt as a placeholder, but in the class constructor you use $this->birth_date.

强制转换后,这将创建一个索引为'birth_date'的数组,该数组与命名参数"dt"不匹配:选择一个或另一个.

Once casted, this will create an array with index 'birth_date', which doesn't match with the named parameter "dt": choose one or the other.

这篇关于SQLSTATE [HY093]:在插入mysql db期间的pdo语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆