Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php错误中向文件追加数据_Php - Fatal编程技术网

在php错误中向文件追加数据

在php错误中向文件追加数据,php,Php,下面的代码将文件中的数据读取到一系列数组中,以及从另一个网页中读取数据,根据表单和数组中的值比较数据,如果未找到匹配项,则将数据附加到文件中 <?php $file = fopen("Student.txt", "a+") or exit("Unable to open file!"); $StudentID = array(); $StudentPassword = array(); $StudentFname = array(); $StudentLname = array(); $S

下面的代码将文件中的数据读取到一系列数组中,以及从另一个网页中读取数据,根据表单和数组中的值比较数据,如果未找到匹配项,则将数据附加到文件中

<?php
$file = fopen("Student.txt", "a+") or exit("Unable to open file!");
$StudentID = array();
$StudentPassword = array();
$StudentFname = array();
$StudentLname = array();
$StudentDOB = array();
$StudentGPA = array();
$x = 0;
$return = 0;

while(!feof($file)){
  $StudentID[$x] = fgets($file);
  $StudentPassword[$x] = fgets($file);
  $StudentFname[$x] = fgets($file);
  $StudentLname[$x] = fgets($file);
  $StudentDOB[$x] = fgets($file);
  $StudentGPA[$x] = fgets($file);
  $x = $x + 1;
  }

  $ID = $_POST["StudentID"];
  $Pass = $_POST["StudentPass"];
  $First = $_POST["StudentFname"];
  $last = $_POST["StudentLname"];
  $DOB = $_POST["D.O.B."];
  $GPA = $_POST["GPA"];

  for ($y=0; $y<$x; $y++){
    if (strcasecmp($ID,$StudentID[$y]) == -2){
    $return = 1;
    break;
  }
  }
  if ($return == 0){
      echo $ID "<br>";
      fputs($ile, $ID);
      fputs($file, $Pass);
      echo $First "<br>";
      fputs($file, $First);
      echo $last <"br>";
      fputs($file, $last);
      echo $DOB "<br>";
      fputs($file, $DOB);
      echo $GPA "<br>";
      fputs($file, $GPA);

      echo "Student successfully added";
 }
  else{
      echo "Duplicate ID found, input rejected";
  }
  fclose($file);
?>

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Student Addition</title>
    </head>
    <body>
        <p>Please return to the <a href ="index.html">Main Menu</a></p>
    </body>
</html>

学生补充
请返回


除了最后一部分,一切都正常。如果没有找到匹配项,它应该追加数据,但就目前情况而言,追加部分不会将数据追加到文件中,使其保持原样。如果有人知道这种情况的原因,我们将非常感谢您的帮助

尝试使用
0
而不是
-2
作为
strcasecmp()
的结果值,因为0表示存在匹配项,在您的情况下,如果存在匹配项,则不应添加它

for ($y=0; $y<$x; $y++){
  if (strcasecmp($ID,$StudentID[$y]) == 0){
  $return = 1;
  break;
 }
}

用于($y=0;$y您是否尝试过将变量$return更改为$result,例如,因为return是一个保留字。我使用了$result而不是$return,并且在您的屏幕未显示的情况下出现了代码错误。我必须假设不是这样。请检查下面的答案。这不再是值错误,这是一个错误,因为它没有打印它o文件周期,无论数据是否被接受。如果有帮助的话,这就是所遇到的问题