如何检查所有POST数据是否都不是空的?
以下是我的代码。即使存在空白字段,也允许INSERT数据。
$stname = $_POST['stname'];
$staddress = $_POST['staddress'];
$stbirth = $_POST['bday'];
$tel = $_POST['tel'];
$email = $_POST['email'];
if(isset($_POST['stname'] && $_POST['staddress'] && $_POST['bday'] && $_POST['tel'] && $_POST['email'])) {
$sql = "INSERT INTO student (Student_Name, Address, Birthday, Telephone, Email) VALUES (:name, :address, :bday, :tel, :email)";
$statement = $con_db->prepare($sql);
$statement->execute(array('name' => $stname, 'address' => $staddress, 'bday' => $stbirth, 'tel' => $tel, 'email' => $email));
if($statement){
echo "Successful!";
} else {
echo "Error occured...";
};
} else {
echo "You did not fill out the required fields.";
}
我甚至尝试了下面的内容。不工作。可能是什么问题?
if(isset($_POST['stname'] , $_POST['staddress'] , $_POST['bday'] , $_POST['tel'] , $_POST['email']))
答案 0 :(得分:0)
在isset
中,您不应使用&&
。试试这个..
if(isset($_POST['stname'] , $_POST['staddress'] , $_POST['bday'] , $_POST['tel'] , $_POST['email'])
and !empty($_POST['stname']) and !empty($_POST['staddress']) and !empty($_POST['bday']) and
!empty($_POST['tel']) and !empty($_POST['email'])
){
//Your codes
}
答案 1 :(得分:0)
更改IF语句并使用已分配的变量。 isset应该只有一个变量。因为否则您将创建一个始终设置的新表达式,因此将返回TRUE。
$stname = $_POST['stname'];
$staddress = $_POST['staddress'];
$stbirth = $_POST['bday'];
$tel = $_POST['tel'];
$email = $_POST['email'];
if($stname != '' && $staddress != '' && $stbirth != '' && $tel != '' && $email != '') {
$sql = "INSERT INTO student (Student_Name, Address, Birthday, Telephone, Email) VALUES (:name, :address, :bday, :tel, :email)";
$statement = $con_db->prepare($sql);
$statement->execute(array('name' => $stname, 'address' => $staddress, 'bday' => $stbirth, 'tel' => $tel, 'email' => $email));
if($statement){
echo "Successful!";
} else {
echo "Error occured...";
};
} else {
echo "You did not fill out the required fields.";
}