如何防止php fwrite()覆盖文件中的一行文本?

如何防止php fwrite()覆盖文件中的一行文本?,php,fwrite,overwrite,Php,Fwrite,Overwrite,我从脚本中提取了以下代码行 $i=0; $j=0; $fp=fopen('C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql','a+');// File where the string is to be written foreach($_POST as $temp)//repeat for all the values passed from the form {

我从脚本中提取了以下代码行

    $i=0;
    $j=0;
    $fp=fopen('C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql','a+');// File where the string is to be written
    foreach($_POST as $temp)//repeat for all the values passed from the form
    {
        if($j==0)
          {     
               $result_set=$mysqli->query("select * from {$_SESSION['table_name_1']} where Copyid=$temp");
               $result_set=$result_set->fetch_array(MYSQL_BOTH);    
               ++$j;

           }
        if($temp!='Drop')// Drop is simply the value of submit buttom
         {
            $date=mysql_query("select now() as current_date_time") or die(mysql_error());
            $date=mysql_fetch_array($date,MYSQL_BOTH);
            $result="\n"."INSERT INTO delete_book_log  // this is the string begining with line break and followed by sql insert statement
                          VALUES(
                                 '{$result_set["Bid"]}',
                                 '$temp',
                                 '{$result_set["Name"]}',
                                 '{$result_set["Publication"]}',
                                 '{$result_set["ISBN"]}',
                                 '{$result_set["Author"]}',
                                 '{$result_set["Edition"]}',
                                 'in',
                                 '{$result_set["Book_Baseid"]}',
                                 '{$date['current_date_time']}'
                                 );";
              fflush($fp);
              fwrite($fp,$result);
              $mysqli->query("Delete from {$_SESSION['table_name_1']} where copyid=$temp");
              $i++;
          }

      }
      fclose($fp);
!![屏幕截图]:

从屏幕截图中可以看到,当选择一条或多条记录并单击下拉按钮时,我希望从数据库中删除记录,但希望将相应的sql insert语句写入文件C:\xampp\htdocs\Lib\u auto\u project\deleted\u records\delete\u log.sql。为此,我编写了上述代码。现在的问题是,当我选择记录并删除它们时,所有记录都会按需要进行。当我在任何其他瞬间执行相同的操作时,我希望类似的sql插入字符串(如上所示存储在$result中)附加到文件C:\xampp\htdocs\Lib\u auto\u project\Deleted\u Records\delete\u log.sql的末尾。这种情况并没有完全发生。相反,以前编写的字符串会被新字符串覆盖。
我已经试了一遍又一遍,但只有最近的字符串覆盖了旧的字符串

尝试使用file\u get\u contents和file\u put\u contents代替fwrite,并将查询附加到数组或字符串:

$fp=file_get_contents('C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql');// File where the string is to be written
foreach($_POST as $temp)//repeat for all the values passed from the form
{
    if($j==0)
      {     
           $result_set=$mysqli->query("select * from {$_SESSION['table_name_1']} where Copyid=$temp");
           $result_set=$result_set->fetch_array(MYSQL_BOTH);    
           ++$j;

       }
    if($temp!='Drop')// Drop is simply the value of submit buttom
     {
        $date=mysql_query("select now() as current_date_time") or die(mysql_error());
        $date=mysql_fetch_array($date,MYSQL_BOTH);
        $result="\n"."INSERT INTO delete_book_log  // this is the string begining with line break and followed by sql insert statement
                      VALUES(
                             '{$result_set["Bid"]}',
                             '$temp',
                             '{$result_set["Name"]}',
                             '{$result_set["Publication"]}',
                             '{$result_set["ISBN"]}',
                             '{$result_set["Author"]}',
                             '{$result_set["Edition"]}',
                             'in',
                             '{$result_set["Book_Baseid"]}',
                             '{$date['current_date_time']}'
                             );";
          $fp .= $result . "\r\n";
          $mysqli->query("Delete from {$_SESSION['table_name_1']} where copyid=$temp");
          $i++;
      }

  }
  file_put_contents($fp);

尝试使用file_get_contents和file_put_contents而不是fwrite,并将查询附加到数组或字符串:

$fp=file_get_contents('C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql');// File where the string is to be written
foreach($_POST as $temp)//repeat for all the values passed from the form
{
    if($j==0)
      {     
           $result_set=$mysqli->query("select * from {$_SESSION['table_name_1']} where Copyid=$temp");
           $result_set=$result_set->fetch_array(MYSQL_BOTH);    
           ++$j;

       }
    if($temp!='Drop')// Drop is simply the value of submit buttom
     {
        $date=mysql_query("select now() as current_date_time") or die(mysql_error());
        $date=mysql_fetch_array($date,MYSQL_BOTH);
        $result="\n"."INSERT INTO delete_book_log  // this is the string begining with line break and followed by sql insert statement
                      VALUES(
                             '{$result_set["Bid"]}',
                             '$temp',
                             '{$result_set["Name"]}',
                             '{$result_set["Publication"]}',
                             '{$result_set["ISBN"]}',
                             '{$result_set["Author"]}',
                             '{$result_set["Edition"]}',
                             'in',
                             '{$result_set["Book_Baseid"]}',
                             '{$date['current_date_time']}'
                             );";
          $fp .= $result . "\r\n";
          $mysqli->query("Delete from {$_SESSION['table_name_1']} where copyid=$temp");
          $i++;
      }

  }
  file_put_contents($fp);

php说a+使用a就足够了,这很好:

开放阅读和写作;将文件指针放在文件末尾。如果文件不存在,请尝试创建它

但请尝试运行名为test.php的代码:


php说a+使用a就足够了,这很好:

开放阅读和写作;将文件指针放在文件末尾。如果文件不存在,请尝试创建它

但请尝试运行名为test.php的代码:


您使用mysqli时没有使用prepared station lol您是否尝试过fopen'C:\xampp…','ab'-在没有b@EugenRieckNo:我只是尝试了一下,它没有帮助。其中Copyid=$temp-有一个唯一的Copyid约束,即每次迭代只能有0或1条记录吗?你能解释一下你想要达到的目标,包括条件吗?我已经编辑了这个问题,以更详细地澄清它,并发布了一个屏幕截图。现在看一下屏幕截图,您可能已经清楚,如果不选择任何内容,就无法删除记录。因此,必须至少有一条记录。您使用mysqli时没有使用准备好的语句。Lol您是否尝试过fopen'C:\xampp…','ab'-在没有b@EugenRieckNo:我只是试了一下,没有用。其中Copyid=$temp-有一个唯一的Copyid约束,即,每次迭代只能有0或1条记录吗?你能解释一下你想要达到的目标,包括条件吗?我已经编辑了这个问题,以更详细地澄清它,并发布了一个屏幕截图。现在看一下屏幕截图,您可能已经清楚,如果不选择任何内容,就无法删除记录。所以必须至少有一条记录。我试过文件内容,但也没用。但是我认为将所有语句放在一个数组中,并将数组写入文件可能会有所帮助。我一定会尝试一下,让你知道。同时,请告诉我你是否认为还有其他方法可以解决这个问题。看来我今天醒来时睡错了床,什么都没用。任何进一步的建议。。我真的被卡住了。这件事发生了!!假设循环迭代了3次,文件中写入了三行。但是,当代码重新执行时,先前写入的行已被覆盖。这不是我想要的。我需要写入后面的行,而不是覆盖前面的行。-1对于命名文本变量$fp b,在这种上下文中创建长文本并在一个位置输出,这是错误的,并且您使用的操作系统应该提供文件缓存,因此fwrite不会立即影响磁盘c,建议文件内容,但不提及文件的结尾,文件将内容放在什么文件中我试过文件内容,也没用。但是我认为将所有语句放在一个数组中,并将数组写入文件可能会有所帮助。我一定会尝试一下,让你知道。同时,请告诉我你是否认为还有其他方法可以解决这个问题。看来我今天醒来时睡错了床,什么都没用。任何进一步的建议。。我真的被卡住了。这件事发生了!!假设循环迭代了3次,文件中写入了三行。但是,当代码重新执行时,先前写入的行已被覆盖。这不是我想要的。我需要写入后面的行,而不是覆盖前面的行。-1对于命名文本变量$fp b,在这种上下文中创建长文本并在一个位置输出,这是错误的,并且您使用的操作系统应该提供文件缓存,以便fwrite不会对磁盘c产生直接影响
在没有提到文件的情况下添加内容,最后,文件将内容放在什么文件中这也没用。但是,当我将文件更改为写入一个新的test.sql文件时,一切正常。但原始文件是一个mysqldump文件。这有什么区别吗?我想问题在于该文件是转储文件。@Jiwan尝试改用它:fopen…,'a'或die'无法打开文件!';这可能是个问题:,编辑答案,看一看。我感觉问题出在转储文件上。它适用于不同的文件。那就行了。天哪!!!!!够了,这也没用。但是,当我将文件更改为写入一个新的test.sql文件时,一切正常。但原始文件是一个mysqldump文件。这有什么区别吗?我想问题在于该文件是转储文件。@Jiwan尝试改用它:fopen…,'a'或die'无法打开文件!';这可能是个问题:,编辑答案,看一看。我感觉问题出在转储文件上。它适用于不同的文件。那就行了。天哪!!!!!够多的奶酪了。