我正在使用PDo和OOP使用xampp将数据插入到数据库mysql中,但是在插入null值时出错,
 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,但是在查询绑定中使用了$Fname,$Lname

变量名称区分大小写。

$Passwords$Password也不同。

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

10-11 08:14