isset()多个字段错误

时间:2014-08-29 19:35:01

标签: php pdo

如何检查所有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']))

2 个答案:

答案 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.";
}