如何检查所有POST数据是否为非空?
下面是我的代码。这样,即使存在空白字段,也可以插入数据。
$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']))
更改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.";
}
在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
}