-1

这个简单的验证问题让我有点发疯!我将它与我一直在学习的一堆其他示例进行比较,但我就是无法理解...

我试图让程序返回简单的消息“请填写所有字段”。如果用户在提交时将任何字段留空。它永远不会得到那个代码。我还有其他类似的例子,所以它一定是我想念的非常简单的东西。谁能告诉我为什么在'if(!empty ...)块之后我没有得到那个'else'语句?

<?php

//PHPAcademy.org Tutorial on PDO

$config['db'] = array(
    'host'      =>  'localhost',
    'username'  =>  'root',
    'password'  =>  'andrew',
    'dbname'    =>  'a_database'
);

$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);

if (isset($_POST['food']) && isset($_POST['calories']) && isset($_POST['healthy_unhealthy'])) {
    $food = $_POST['food'];
    $calories = $_POST['calories'];
    $healthy_unhealthy = $_POST['healthy_unhealthy'];

    echo $food . $calories . $healthy_unhealthy;

    if (!empty($food) && !empty($calories) && !empty($healthy_unhealthy)) {
        $query = $db->prepare("INSERT INTO food (food, calories, healthy_unhealthy) VALUES (:food, :calories, :healthy)");
        $query->bindValue(':food', $food);
        $query->bindValue(':calories', $calories);
        $query->bindValue(':healthy', $healthy_unhealthy);

        if ($query->execute()) {
            echo '<br /><strong>Record Added!</strong><br />';
        } else {
            echo '<br />Oh no, there was a problem!<br />';
        }
    } else {
        echo 'Please complete all fields.';
    }
}


?>

<form action="connect.php" method="POST">
    Food Type: <br />
    <input type="text" name="food"/><br /><br />
    Calories: <br />
    <input type="number" min="0" name="calories"/><br /><br />
    Healthy/Unhealthy? <br />
    <input type="radio" name="healthy_unhealthy" value="h"/>Healthy<br />
    <input type="radio" name="healthy_unhealthy" value="u"/>Unhealthy<br /><br />
    <input type="submit" value="Submit" />
</form>

我认为这与 isset 也检查空值这一事实有关,但在下面的示例中,它一切正常,我看不出有什么区别......

<?php

// Pilot Name, Currency Name, Currency Date, Interval (minutes)

if (isset($_POST['pilot']) && isset($_POST['currency']) && isset($_POST['last_date']) &&
        isset($_POST['interval'])) {
            $pilot = $_POST['pilot'];
            $currency = $_POST['currency'];
            $last_date = strtotime($_POST['last_date']);
            $interval = $_POST['interval'];

    if (!empty($pilot) && !empty($currency) && !empty($last_date) &&
            !empty($interval)) {

        echo 'Hello <strong>' . $pilot . '</strong>, your ' . $currency . ' is due on ' . date('d-m-y', $last_date + ($interval * 24 * 60 * 60)) .
            '<br />';
        echo 'Your last currency was ' . date('d-m-Y', $last_date) . '<br /><br />';

        } else {
            echo 'Please complete all fields.';
        }
}




?>

<form action="currency.php" method="POST">
    Enter your name:<br />
    <input type="text" name="pilot"/><br/>
    Enter the last date of your currency (dd-mm-yy):<br />
    <input type="date" name="last_date"/></br/>
    Enter the currency type:<br />
    <input type="text" name="currency"/><br/>
    Enter the currency interval in days: <br />
    <input type="text" name="interval"/><br/>
    <input type="submit" value="submit">
</form>

感谢您的任何建议...

4

5 回答 5

2

即使你没有在输入字段中进行任何测试,提交按钮仍然会发送数据

<input name="sth" >

结果是

$_POST['sth'] == ""

它是一个变量,它是一个字符串,它的长度只是 0。您需要比isset(.)函数更复杂的值测试。例如,您可以使用strlen(.)(或empty(.))。

对于复选框而言,情况并非如此,它不作为传统输入,并且不创建$_POST['checkboxvalue']值,这使得第一个 if 语句 - always false

于 2013-08-11T08:47:42.467 回答
0

你确定你通过了:

    if (isset($_POST['food']) && isset($_POST['calories']) && isset($_POST['healthy_unhealthy']))

将 else 子句也复制到那里。澄清一下,这些字段之一可能未在 $_POST 中设置 - 常见于复选框和单选框。

于 2013-08-11T08:45:53.467 回答
-1

对于 health_unhealthy 广播组,请尝试以下检查:

isset($_POST['healthy_unhealthy'])
于 2013-08-11T08:44:53.113 回答
-1

当第二个条件为真时,第一个 if 条件也可能为真。尝试这个:

if (isset($_POST['food']) && isset($_POST['calories']) && isset($_POST['healthy_unhealthy'])) {
     $food = $_POST['food'];
     $calories = $_POST['calories'];
     $healthy_unhealthy = $_POST['healthy_unhealthy'];

    echo $food . $calories . $healthy_unhealthy;

    if (!empty($food) && !empty($calories) && !empty($healthy_unhealthy)) {
         ...
    } else {
        echo 'Please complete all fields.';
    }
} else {
    echo 'Script parameters missing.';
}
于 2013-08-11T08:50:10.980 回答
-3

因为特定if语句中的条件总是计算为TRUE.

于 2013-08-11T08:41:55.063 回答