【问题标题】:How can I check if all variables meet the requirements to put data into file?如何检查所有变量是否满足将数据放入文件的要求?
【发布时间】:2018-02-05 19:45:13
【问题描述】:

如果所有变量都继续进行文件处理,我需要检查它们是否满足要求。

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$s_id = $_POST['student_id'];
$tuition = $_POST['tuition'];
$payment = $_POST['payment'];

//Checking for Errors
if ($_SERVER["REQUEST_METHOD"] == "POST"){
    //Checking Values
    echo '<ul class="error">';
    if(strlen($fname)<2){
        echo '<li>'."First name must be 2 or more characters in length".'</li>';
    }
    if(strlen($lname)<3 || strlen($lname)>12){
        echo '<li>'."Last name must be between 3 and 12 characters in length".'</li>';
    }
    if(strlen($s_id)!=9){
        echo '<li>'."Student id must be exactly 9 characters in length".'</li>';
    }
    if($tuition < 2000 || $tuition > 10000){
        echo '<li>'."Tuition must be between 2000 and 10000".'</li>';
    }
    echo '</ul>';

这部分工作,因为它应该报告遇到了什么错误。在此之后,如果输出成功消息并将数据放入文件中,我需要确保所有值都是正确的。

//Success
    if($fname == true && $lname == true && $s_id == true && $tuition == true){
        echo '<ul class="success">';
        echo '<li>'."Payment Successful!".'</li>';
        echo '</ul>';

        //File Handling
        $line = array($fname, $lname, $s_id, $tuition, $payment); //Creates a line to append to the file.
        $handle = fopen("log.txt", "a+"); //Open for reading and writing; place the file pointer at the end of the file.
        fputcsv($handle, $line); //Puts the values into the file.
        fclose($handle); //Close the file.
    }
}

我在检查所有变量是否满足我使用的if($fname == true &amp;&amp; $lname == true &amp;&amp; $s_id == true &amp;&amp; $tuition == true) 的要求时遇到问题,但即使出现错误似乎也可以。我到底做错了什么?

【问题讨论】:

  • 在脚本开头使用标志$success = true;,然后在所有错误条件中添加$success = false;...然后在末尾而不是if($fname == true &amp;&amp; $lname == true &amp;&amp; $s_id == true &amp;&amp; $tuition == true){,您现在只需使用if($success){
  • $s_id = '1'; var_dump($s_id == true);
  • @Hackerman 谢谢。另一个简单的问题,格式是否可以,或者将 HTML 与 PHP 分开会更好吗?
  • 既然你是这样用的,那就继续这样的编码方式,直到你掌握它……然后慢慢转向像CodeIgniter这样的mvc框架……还有,另一种改进的方法您的代码避免使用多个 echo 语句...最好创建一个 $errorMessage = "" 变量,并在每个条件中使用字符串连接...并在最后回显变量,只需一次 echo $errorMessage;

标签: php html if-statement fputcsv


【解决方案1】:

解决方案是这样的:

$problems = [];
if(strlen($fname)<2){
    $problems[] = "First name must be 2 or more characters in length";
}
if(strlen($lname)<3 || strlen($lname)>12){
    $problems[] = "Last name must be between 3 and 12 characters in length";
}
if(strlen($s_id)!=9){
    $problems[] = "Student id must be exactly 9 characters in length";
}
if($tuition < 2000 || $tuition > 10000){
    $problems[] = "Tuition must be between 2000 and 10000";
}

if (count($problems)) {
    echo '<ul class="error">';
    foreach ($problems as $problem) {
        echo '<li>'.$problem.'</li>';
    }
    echo '</ul>';
}
if (empty($problems)) {
    // Success action here
}

这将列出所有问题,如果有问题则打印它们,如果没有问题则执行成功操作。

当您使用框架时,打印列表将在一个名为“视图”的单独文件中完成,这将进一步将 html 与验证逻辑分开。

编辑:“即使出现错误,它似乎也能正常运行。我到底做错了什么?”

在这种情况下,我认为你不是唯一一个做错事的人。如果我是 PHP,我会做相反的事情:不让任何东西通过,即使没有错误。 以下是发生的事情:

  • 假设 $fname 是“E”。这不是有效的输入。
  • 您询问 PHP 是否 $fname 为真。这意味着询问“E”是否为真。
  • PHP 认为“E”似乎足够真实。

所有参数都会发生类似的情况。只有一个完全空的 $fname 被拒绝。对于学费,PHP 只拒绝免费课程。

发生这种情况的原因是因为 PHP 是 "weak typed"

大多数其他语言会简单地得出“E”与真不完全相同的结论,因此拒绝输入。话又说回来,他们还会得出结论,“real_file_name.txt”与 true 不完全相同,并且还会拒绝您认为有效的输入。

【讨论】:

  • 谢谢,我已经修复了它,但你的例子比我所做的更优化。我稍后会尝试这样做。
【解决方案2】:

我建议您使用代码示例 isset(var) 并使用 var $valid 检查条件是否为真,我希望这可以帮助您:

<?php
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$s_id = $_POST['student_id'];
$tuition = $_POST['tuition'];
$payment = $_POST['payment'];
$valid="";

if (isset($fname, $lname, $s_id, $tuition, $payment)) {
    $valid="1";
    echo '<ul class="error">';
    if(strlen($fname)<2){
        echo '<li>'."First name must be 2 or more characters in length".'</li>';
        $valid="0";
    }
    if(strlen($lname)<3 || strlen($lname)>12){
        echo '<li>'."Last name must be between 3 and 12 characters in length".'</li>';
        $valid="0";
    }
    if(strlen($s_id)!=9){
        echo '<li>'."Student id must be exactly 9 characters in length".'</li>';
        $valid="0";
    }
    if($tuition < 2000 || $tuition > 10000){
        echo '<li>'."Tuition must be between 2000 and 10000".'</li>';
        $valid="0";
    }
    echo '</ul>';
}
if ($valid == "1") {
    echo '<ul class="success">';
    echo '<li>'."Payment Successful!".'</li>';
    echo '</ul>';
    $line = array($fname, $lname, $s_id, $tuition, $payment);
    $handle = fopen("log.txt", "a");
    fwrite($handle, $line);
    fclose($handle);
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-20
    • 2016-04-26
    • 2013-03-06
    • 1970-01-01
    • 2019-07-08
    • 1970-01-01
    相关资源
    最近更新 更多