我有以下语句用于验证 PHP 中的空字段,它工作得很好:
if (isset($_SESSION['logged_in'])) {
if (isset($_POST['room'], $_POST['area'])) {
$room = $_POST['room'];
$area = $_POST['area'];
if (empty($room) or empty($area)) {
$error = 'Required!';
} else {
$query = $pdo->prepare('INSERT INTO apartments (apartments_room, apartments_area) VALUES (?, ?)');
$query->bindValue(1, $room);
$query->bindValue(2, $area);
$query->execute();
header('Location: index.php');
}
}
我现在已经打开了数据库中的更多列,因此在 html 中也有更多的插入字段来验证。我的问题只是在这种情况下检查多个空字段时如何制定用于验证的 if 语句。
因此,例如此代码不再起作用:
if (isset($_SESSION['logged_in'])) {
if (isset($_POST['room'], $_POST['area'], $_POST['rent'], $_POST['address'], $_POST['floor'], $_POST['city'], $_POST['freeFromDate'])) {
$room = $_POST['room'];
$area = $_POST['area'];
$rent = $_POST['rent'];
$address = $_POST['address'];
$floor = $_POST['floor'];
$city = $_POST['city'];
$freeFromDate = $_POST['freeFromDate'];
if (empty($room) || empty($area) || empty($rent) || empty($address) || empty($floor) || empty($city) || empty($freeFromDate)) {
$error = 'Required!';
} else {
$query = $pdo->prepare('INSERT INTO apartments (apartments_room, apartments_area, apartments_rent, apartments_address, apartments_floor, apartments_city, apartments_freeFromDate) VALUES (?, ?, ?, ?, ?, ?, ?)');
$query->bindValue(1, $room);
$query->bindValue(2, $area);
$query->bindValue(3, $rent);
$query->bindValue(4, $address);
$query->bindValue(5, $floor);
$query->bindValue(6, $city);
$query->bindValue(7, $freeFromDate);
$query->execute();
header('Location: index.php');
}
}
如何在这里制定语法?
谢谢!