我正在使用一个简单的验证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
为空时,程序可以正常工作,但是如果我输入值到我的字段,它总是返回echo "Must input higher value than"; exit;
,即使我输入的值高于15000.这有什么问题?
答案 0 :(得分: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')");
}
}