我已經在下面發佈了我的代碼。我試圖將數據插入數據庫。問題是如果用戶沒有選擇一個文本字段,它不會將剩餘的數據插入到數據庫中。例如,我有一個註冊表單。用戶應填寫所有文本框,但userprofilepicture是可選的。所以,如果用戶沒有選擇他/她的圖像文件,它不會插入剩餘的數據。如果一個文本字段爲空,如何插入數據?如果一個textfiled爲空,pdo插入不起作用
if(isset($_POST['sub_values']))
{
$c_uname = $_POST['uname'];
// some codes
// some codes
// some codes
$token = md5(uniqid(mt_rand(), true));
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0)
{
$tmpName = $_FILES['image']['tmp_name'];
$fp = fopen($tmpName, 'r');
$data = fread($fp, filesize($tmpName));
$data = addslashes($data);
fclose($fp);
try
{
$stmt = $conn->prepare("INSERT INTO ebusers (UserName, UserPassword, ConfirmPassword, FirstName, LastName, Address, UserEmail, UserPhone, Gender, UserProfilePicture, CreatedOn, isAdmin, UserSecretID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, '$data', NOW(), ?, ?)");
$conn->errorInfo();
$stmt->bindParam('1', $c_uname, PDO::PARAM_STR);
$stmt->bindParam('2', $c_pwd, PDO::PARAM_STR);
$stmt->bindParam('3', $c_cpwd, PDO::PARAM_STR);
$stmt->bindParam('4', $c_name, PDO::PARAM_STR);
$stmt->bindParam('5', $c_lname, PDO::PARAM_STR);
$stmt->bindParam('6', $c_address, PDO::PARAM_STR);
$stmt->bindParam('7', $c_email, PDO::PARAM_STR);
$stmt->bindParam('8', $c_phone, PDO::PARAM_STR);
$stmt->bindParam('9', $c_fm, PDO::PARAM_STR);
$stmt->bindParam('10', $type, PDO::PARAM_STR);
$stmt->bindParam('11', $token, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
'Error : ' .$e->getMessage();
}
}
if($stmt)
{
echo "<script> alert('Registration successfully completed. You can login now'); </script>";
echo "<script> location.href='index.php' </script>";
}
else
{
echo "<script> alert('Registration Unsuccessfull. Try again or contact your administrator'); </script>";
echo "<script> location.href='createuser.php' </script>";
}
}
表結構
CREATE TABLE IF NOT EXISTS `ebusers` (
`UserID` int(11) NOT NULL AUTO_INCREMENT,
`UserName` char(25) NOT NULL,
`UserPassword` varchar(30) NOT NULL,
`ConfirmPassword` varchar(30) NOT NULL,
`FirstName` varchar(50) NOT NULL,
`LastName` varchar(50) NOT NULL,
`Address` varchar(100) NOT NULL,
`UserEmail` varchar(200) NOT NULL,
`UserPhone` varchar(50) NOT NULL,
`Gender` char(1) NOT NULL,
`UserProfilePicture` longblob NOT NULL,
`UserSecretID` varchar(40) NOT NULL,
`ActiveStatus` enum('Y','N') NOT NULL,
`CreatedOn` datetime NOT NULL,
`ModifiedOn` datetime NOT NULL,
`isAdmin` enum('Y','N') NOT NULL,
`UserToken` varchar(300) NOT NULL,
PRIMARY KEY (`UserID`),
UNIQUE KEY `UserName` (`UserName`,`UserEmail`,`UserPhone`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=17 ;
如果您使用'bindValue()'來代替它是否工作?你也可以粘貼數據庫表結構(例如你的可選字段是否允許爲null) – Scuzzy
@Scuzzy:我已經發布了這個代碼的表結構。 – Karuppiah