我試圖通過讓用戶填寫已驗證的表單來更新頁面上的表格。出於某種原因,當我添加驗證if語句時,表單除了在用戶單擊「添加新」時刷新之外不會執行任何操作。如果我刪除了if語句,表就會更新。我錯過了什麼?由於if語句,PHP表單無法驗證或發送?
<?php
session_start();
require_once "db.php";
if (isset ($_POST['url']) && isset ($_POST['email'])
&& isset ($_POST['length']) && isset ($_POST['rating'])) {
//Reject empty form fields
if (strlen($_POST['url']) < 1 || strlen($_POST['email']) <1 || strlen ($_POST['length']) <1 || strlen($_POST['rating']) < 1) {
$_SESSION['error'] = 'All values are required';
header('Location: add.php');
return;
}
//Reject urls that are not http or https
$url = $_POST['url'];
if (strpos($url,"http://") !== 0 && strpos($url, "https://") !== 0) {
$_SESSION['error'] = 'Error in input data';
header('Location: add.php');
return;
}
//Reject emails without '@' symbol
if (strpos($_POST['email'], "@") === false) {
$_SESSION['error'] = 'Error in input data';
header('Location: add.php');
return;
}
//Reject track lengths or rating numbers that are too small
if ($_POST['length'] +0 < 1 || $_POST['rating'] +0 < 1) {
$_SESSION['error'] = 'Error in input data';
header('Location: add.php');
return;
}
$sql = "INSERT INTO videos (url, email, length, rating)
VALUES (:url, :email, :length, :rating)";
$stmt = $pdo->prepare($sql);
$stmt->execute(array(
':url' => $_POST ['url'],
':email' => $_POST ['email'],
':length' => $_POST['length'],
':rating' => $_POST['rating']));
$_SESSION['success'] = 'Record Added';
header('Location: index.php') ;
return;
}
?>
<p>Add A New Video</p>
<form method="post">
<p>URL:
<input type="text" name="url"></p>
<p>Email:
<input type="text" name="email"></p>
<p>Length:
<input type="text" name="length"></p>
<p>Rating:
<input type="text" name="rating"></p>
<p><input type="submit" value="Add New"/>
<a href="index.php">Cancel</a></p>
</form>
您是否檢查(打印)了變量的值以查看它們存儲的內容? – 2014-03-19 15:23:41