Reputation: 3
i'm using PDo with OOP to insert data into database mysql using xampp , i got error for inserting null values , this code of sql be note the id auto increment , it works fine
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;
this code for connection with database it works fine
<?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;
}
}
?>
consumer class , that has the exception error
<?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);
?>
Upvotes: 0
Views: 91
Reputation: 1177
Be careful with variables.
You're defining function parameters as $FName,$LName
, but you're using $Fname,$Lname
in query binding.
Variable names are case-sensitive.
$Passwords
and $Password
are different too.
Upvotes: 1