2016-03-07 82 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