2016-03-07 4 views
0

我用簡單的驗證PHP程序的工作,我有2個文件,1個用於顯示和1個控制這些都是我的代碼:總是返回一個聲明

<form action="comp.php" method="post">  

       <input type="text" name="reading" placeholder="kilometer reading"> 
       <input type="date" name="date1" placeholder="date"> 
       <input type="text" name="suggest" placeholder="suggested kilometer"> 
       <input type="text" name="part" placeholder="spare part"> 

      <input type="submit" class="button" name="btnsubmit" value="Submit"> 
     </form> 

,這是對我的控制:

$date1 = date('Y-m-d', strtotime($_POST['date1'])); 
$reading = $_POST['reading']; 
$suggest = $_POST['suggest']; 
$part =$_POST['part']; 

$sql = "SELECT reading FROM sched ORDER BY reading DESC LIMIT 1"; 
$result = mysqli_query($sqli, $sql); 


     if (empty($_POST['reading'])) 
     { 
     echo "No Input "; 
     exit; 
     } 
     elseif ($_POST['reading'] < $result) 
     { 
     echo "Must input higher value than"; 
     exit; 
     } 
     elseif ($_POST['reading'] > $result) 
     { 
      if (($_POST['date1']) == "1970-01-01") 
      { 
      echo "no date input"; 
      exit; 
      } 
      else 
      { 
       $query = mysqli_query($sqli,"INSERT INTO sched (date,reading,suggest,part) VALUES ('$date1','$reading','$suggest','$part')"); 
      } 
     } 
} 


mysqli_close($sqli); 
header("Location: log.php"); 

我目前的最高讀數是'15000'。 當我的字段reading爲空時,程序可以正常工作,但是如果我向我的字段輸入值,即使輸入的值高於15000,也總是返回echo "Must input higher value than"; exit;.這是什麼問題?

+1

如果您嘗試獲取結果集,將更好地工作,目前'$ result'將等於或者TRUE;或'false'作爲mysqli_query()返回查詢執行的狀態,而不是返回結果集。當所有其他都失敗時[閱讀Flippin手冊](http://php.net/manual/en/function.mysql-query.php) – RiggsFolly

+0

使用提示信息的錯誤消息是非常好的做法,以'echo'開始您的輸入值['。$ _ POST ['reading']。']低於['.result。']';' –

回答

1

mysqli_query()只是提交查詢到數據庫進行編譯和執行。如果查詢中出現錯誤,則返回FALSE,因此在繼續之前需要測試返回的值。

如果狀態不爲FALSE,那麼$ result將爲mysqli_result對象,您可以使用該對象檢索由查詢生成的結果行。

$reading = $_POST['reading']; 
$suggest = $_POST['suggest']; 
$part = $_POST['part']; 

$sql = "SELECT reading FROM sched ORDER BY reading DESC LIMIT 1"; 
$result = mysqli_query($sqli, $sql); 
if ($result === FALSE) { 
    echo mysql_error(); 
    exit; 
} 

$row = mysqli_fetch_object($result); 
if (empty($_POST['reading'])) { 
    echo "No Input "; 
    exit; 
} 

if ($_POST['reading'] <= $row->reading) { 
    echo "Must input higher value than {$row->reading}"; 
    exit; 
} 

if ($_POST['reading'] > $row->reading) { 
    if (($_POST['date1']) == "1970-01-01") { 
     echo "no date input"; 
     exit; 
    } else { 
     $query = mysqli_query($sqli,"INSERT INTO sched 
            (date,reading,suggest,part) 
            VALUES 
            ('$date1','{$row->reading}','$suggest','$part')"); 
    } 
} 

如果你不喜歡簡單的對象從查詢中回報,你可以做

$row = mysqli_fetch_assoc($result); 
    if (empty($_POST['reading'])) { 
     echo "No Input "; 
     exit; 
    } 

    if ($_POST['reading'] <= $row['reading']) { 
     echo "Must input higher value than {$row['reading']}"; 
     exit; 
    } 

    if ($_POST['reading'] > $row['reading']) { 
     if (($_POST['date1']) == "1970-01-01") { 
      echo "no date input"; 
      exit; 
     } else { 
      $query = mysqli_query($sqli,"INSERT INTO sched 
             (date,reading,suggest,part) 
             VALUES 
             ('$date1','{$row['reading']}','$suggest','$part')"); 
     } 
    } 
+0

非常感謝!我有一個問題是$ row->閱讀面向對象?我是對的?如何將其轉換爲程序 –

+0

是的,'mysqli_fetch_object($ result)'返回一行作爲一個對象,SELECT中的每一列作爲'$ row'的屬性返回 – RiggsFolly