我正在使用PDO在PHP中使用MySQL进行准备好的查询。查询执行时没有错误,但是一个参数没有正确传递它的值。它是一个空白字段,但不是NULL。这真的很奇怪,因为我似乎输入的代码与其他代码一样,当然工作正常。非常令人沮丧。
$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;
}
print_r的输出($ _ POST);是:数组([email] => test@test.com [pass] =>传递[pass_confirm] =>传递[f_name] => fefs [l_name] => sfasafs [address_1] => sfafsa [address_2] => dsfsafsaf [suburb] => dffdsa [city] => dsffdf [country] => sfaafdsa [phone] => fsaafssaf [submit] =>注册)
问题是数据库填充了新记录,但密码字段是空白。我删除了散列以简化操作。
以下是用户表的SQL:
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 ;
答案 0 :(得分:3)
你偶然在$pass
中定义db_connect.php
吗?如果这样做,它将覆盖您的原始声明。