如何检查所有变量是否满足将数据放入文件的要求?
How can I check if all variables meet the requirements to put data into file?
我需要检查是否所有变量都满足要求,如果它们继续进行文件处理。
$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 && $lname == true && $s_id == true && $tuition == true)
,但即使出现错误,它似乎也能正常运行。我到底做错了什么?
我建议您使用 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);
}
?>
解决方案是这样的方法:
$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
}
这将列出所有问题,如果有问题则打印它们,如果没有问题则执行成功操作。
当您使用框架时,打印列表将在一个名为 "view" 的单独文件中完成,这会进一步将 html 与验证逻辑分开。
编辑: "it seems to go though even with errors. What exactly am I doing wrong?"
在这种情况下,我认为您不是唯一做错事的人。如果我是 PHP,我会做相反的事情:不让任何东西通过,即使没有错误。
这是发生了什么:
- 假设 $fname 是 "E"。这不是有效的输入。
- 你问 PHP $fname 是否为真。这意味着询问 "E" 是否为真。
- PHP 认为 "E" 似乎足够真实。
所有参数都会发生类似的情况。只有完全空的 $fname 才会被拒绝。对于学费,PHP只拒绝免费课程。
发生这种情况的原因是因为 PHP 是 "weak typed"。
大多数其他语言会简单地断定 "E" 与 true 不完全相同,因此拒绝输入。然后,他们还会得出 "real_file_name.txt" 与 true 不完全相同的结论,并且还会拒绝您认为有效的输入。
我需要检查是否所有变量都满足要求,如果它们继续进行文件处理。
$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 && $lname == true && $s_id == true && $tuition == true)
,但即使出现错误,它似乎也能正常运行。我到底做错了什么?
我建议您使用 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);
}
?>
解决方案是这样的方法:
$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
}
这将列出所有问题,如果有问题则打印它们,如果没有问题则执行成功操作。
当您使用框架时,打印列表将在一个名为 "view" 的单独文件中完成,这会进一步将 html 与验证逻辑分开。
编辑: "it seems to go though even with errors. What exactly am I doing wrong?"
在这种情况下,我认为您不是唯一做错事的人。如果我是 PHP,我会做相反的事情:不让任何东西通过,即使没有错误。 这是发生了什么:
- 假设 $fname 是 "E"。这不是有效的输入。
- 你问 PHP $fname 是否为真。这意味着询问 "E" 是否为真。
- PHP 认为 "E" 似乎足够真实。
所有参数都会发生类似的情况。只有完全空的 $fname 才会被拒绝。对于学费,PHP只拒绝免费课程。
发生这种情况的原因是因为 PHP 是 "weak typed"。
大多数其他语言会简单地断定 "E" 与 true 不完全相同,因此拒绝输入。然后,他们还会得出 "real_file_name.txt" 与 true 不完全相同的结论,并且还会拒绝您认为有效的输入。