gpt4 book ai didi

php - PDO异常 SQLSTATE[23000] :in php

转载 作者:行者123 更新时间:2023-11-29 10:47:21 25 4
gpt4 key购买 nike

我使用 PDo 和 OOP 来使用 xampp 将数据插入到数据库 mysql 中,但插入空值时出现错误, 这段sql代码注意id自动递增,它工作正常

     CREATE TABLE `consumer` (

`Consumer_ID` int(8) NOT NULL,

`FName` varchar(20) NOT NULL,

`LName` varchar(20) NOT NULL,

`Email` varchar(35) NOT NULL,

`Passwords` varchar(255) NOT NULL,

`Gender` char(1) NOT NULL,

`Birth_Year` int(4) DEFAULT NULL,

`City` varchar(20) NOT NULL,

`Neighborhood` varchar(20) NOT NULL,

`Mobile_Number` int(11) NOT NULL,

`Contact_Preference` varchar(15) NOT NULL,

`Status` char(1) DEFAULT 'a',

`Longitude` float(10,6) DEFAULT NULL,

`Latitude` float(10,6) DEFAULT NULL,

`Join_Date` timestamp NULL DEFAULT CURRENT_TIMESTAMP,

`Last_Login_Date` timestamp NULL DEFAULT NULL

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

ALTER TABLE `consumer`
ADD PRIMARY KEY (`Consumer_ID`),
ADD UNIQUE KEY `Email` (`Email`);
ALTER TABLE `consumer`
MODIFY `Consumer_ID` int(8) NOT NULL AUTO_INCREMENT;

这段与数据库连接的代码工作正常

    <?php
class dbconnection{
Private $server = "localhost";
Private $database = "*****";
Private $userName = "*****";
Private $password = "*****";
public function connect()
{
try{
$db = new PDO("mysql:host=$this->server;dbname=$this->database",
$this->userName, $this->password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $db;
}
catch(PDOException $e){
echo "Connection failed: " . $e->getMessage();
}
}
public function disconnect(){
$db = null;
return $db;
}
}
?>

消费者类,出现异常错误

    <?php
require 'dbconnection.class.php';
class consumer
{
Public function register($FName,$LName,$Email,$Passwords,$Gender,$Birth_Year,$City,$Neighborhood,$Mobile_Number,$Contact_Preference,$Longitude,$Latitude)
{
try
{
$connection = new dbconnection;
$db = $connection->connect();
$Query = $db->prepare("INSERT INTO consumer(FName,LName,Email,Passwords,Gender,Birth_Year,City,Neighborhood,Mobile_Number,Contact_Preference,Longitude,Latitude)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
$Query->bindParam(1,$Fname);
$Query->bindParam(2,$Lname);
$Query->bindParam(3,$Email);
$Query->bindParam(4,$Password);
$Query->bindParam(5,$Gender);
$Query->bindParam(6,$Birth_Year);
$Query->bindParam(7,$City);
$Query->bindParam(8,$Neighborhood);
$Query->bindParam(9,$Mobile_Number);
$Query->bindParam(10,$Contact_Preference);
$Query->bindParam(11,$Longitude);
$Query->bindParam(12,$Latitude);
$Query->execute();
return $db->lastInsertId();
$db = $connection->disconnect();
} catch (PDOException $e) {
exit($e->getMessage());
}
}
}
$test = new consumer;
$test->register('ahmed','ahmed','ahmed','ahmed','4','2018','cairo','naser',01121980528,'now',5,9);
?>

最佳答案

小心变量。您将函数参数定义为 $FName,$LName,但在查询绑定(bind)中使用 $Fname,$Lname

变量名称区分大小写。

$Passwords$Password 也不同。

关于php - PDO异常 SQLSTATE[23000] :in php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44318127/

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