gpt4 book ai didi

php - SQLSTATE[HY093] : pdo statement during insert into mysql db

转载 作者:可可西里 更新时间:2023-11-01 08:37:00 27 4
gpt4 key购买 nike

这是我实现插入数据库的php代码:

<?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 的地方

    $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” 不匹配:选择一个或另一个。

关于php - SQLSTATE[HY093] : pdo statement during insert into mysql db,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9234425/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com