2014-03-19 51 views
0

我試圖通過讓用戶填寫已驗證的表單來更新頁面上的表格。出於某種原因,當我添加驗證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> 
+0

您是否檢查(打印)了變量的值以查看它們存儲的內容? – 2014-03-19 15:23:41

回答

0

我會說你使用strpos驗證容易出現問題。嘗試使用像這樣的正則表達式:

if (! preg_match('/[^@\s]{3,}@([-a-z0-9]{2,}\.)+[a-z]{2,}/', $_POST['e_mail'])) { $_SESSION['error'] = 'Error in input data'; } 
+0

感謝您的建議。我做了這個改變,但它並沒有解決我原來的問題。 – user3384487

0

查看下面的代碼是否適合您?我已經稍微修改了長度評分字段以及電子郵件字段的條件。祝你好運!

<?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'; 
     die(header('Location: add.php')); 
    } 

    //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'; 
     die(header('Location: add.php')); 
    } 

    //Reject emails without '@' symbol 
    if (FILTER_VAR($_POST['email'], FILTER_VALIDATE_EMAIL)) { 
     $_SESSION['error'] = 'Error in input data'; 
     die(header('Location: add.php')); 
    } 

    //Reject track lengths or rating numbers that are too small 
    if ($_POST['length'] < 1 || $_POST['rating'] < 1 || $_POST['length'] > 1000 || $_POST['rating'] > 1000) { 
     $_SESSION['error'] = 'Error in input data'; 
     die(header('Location: add.php')); 
    } 

    $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') ; 

} 

?> 

<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> 
+0

你的代碼的反應方式與我的做法相同:/刷新頁面但沒有捕捉到任何東西。如果我註釋掉驗證if語句,它允許我輸入數據並重定向到index.php。 – user3384487

+0

@ user3384487爲什麼不只是嵌套if ... else語句呢?如同一個又一個?此外,您需要爲表單添加一個* action =「」*屬性,該值是處理的URL或頁面名稱。在這種情況下,由於您正在處理同一頁面,因此可以將該值留空。 – 9997

+0

@ user3384487我會使用action =「<?php print $ _SERVER ['PHP_SELF'];?>」 – 2014-03-19 15:20:59