tags:

views:

602

answers:

1

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 ;
+1  A: 

You don't happen to define $pass in db_connect.php by any chance do you? If you do, it will overwrite your original declaration.

etheros
One easy way around this being to move all the code currently present into a function, then call that function... PHP can't see global variables (except the superglobals) without the global key word.
R. Bemrose
Oh man, <facepalm> I'm sure that's it. Thanks to everyone for your help. I might move it into a function. Guess what, for testing purposes the mysql password is the empty string which explains the blank password field. There I was thinking that part "worked" and leaving it out of the equation. Thanks etheros, Bemrose and also James for your help earlier.
Edward Williams