I am using PDO to do prepared queries in php with MySQL. The query executes with no errors but one parameter does not pass it's value properly. It come sup as a blank field, but not NULL. It's really weird as it seems that I entered the code the same as the others which of course work fine. Really frustrating.
$f_name = $_POST['f_name'];
$l_name = $_POST['l_name'];
$email = $_POST['email'];
$pass = $_POST['pass'];
$address1 = $_POST['address_1'];
$address2 = $_POST['address_2'];
$suburb = $_POST['suburb'];
$city = $_POST['city'];
$country = $_POST['country'];
$phone = $_POST['phone'];
include('db_connect.php');
$stmt = $db_connection->prepare('INSERT INTO tblUser (userID,fName, lName, email, password, addressLine1, suburb, city, country, phone) VALUES( NULL, :f_name, :l_name, :email, :pass, :address1, :suburb, :city, :country, :phone)');
$stmt->bindParam(':f_name', $f_name, PDO::PARAM_STR);
$stmt->bindParam(':l_name', $l_name, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->bindParam(':pass', $pass, PDO::PARAM_STR);
$stmt->bindParam(':address1', $address1, PDO::PARAM_STR);
//$stmt->bindParam(':address2', $address2, PDO::PARAM_STR);
$stmt->bindParam(':suburb', $suburb, PDO::PARAM_STR);
$stmt->bindParam(':city', $city, PDO::PARAM_STR);
$stmt->bindParam(':country', $country, PDO::PARAM_STR);
$stmt->bindParam(':phone', $phone, PDO::PARAM_STR);
try {
$stmt->execute();
print_r($stmt);
$result = $stmt->fetchAll();
}
catch (Exception $e) {
echo $e;
}
The output of print_r($_POST); is: Array ( [email] => [email protected] [pass] => pass [pass_confirm] => pass [f_name] => fefs [l_name] => sfasafs [address_1] => sfafsa [address_2] => dsfsafsaf [suburb] => dffdsa [city] => dsffdf [country] => sfaafdsa [phone] => fsaafssaf [submit] => Register )
The problem is that the database is populated with a new record but the password field is a blank. I removed hashing to simplify things.
Here is the SQL of the user table:
CREATE TABLE IF NOT EXISTS `tbluser` (
`userID` int(11) NOT NULL AUTO_INCREMENT,
`password` varchar(40) NOT NULL,
`email` varchar(60) NOT NULL,
`addressLine1` varchar(30) DEFAULT NULL,
`addressLine2` varchar(30) DEFAULT NULL,
`suburb` varchar(30) DEFAULT NULL,
`city` varchar(30) DEFAULT NULL,
`country` varchar(40) DEFAULT NULL,
`phone` varchar(25) DEFAULT NULL,
`fName` varchar(30) DEFAULT NULL,
`lName` varchar(30) NOT NULL,
`admin` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`userID`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `email_3` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;