I have extracted following lines of code from my script.
$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);
![screen shot]: http://i.stack.imgur.com/dOzSj.jpg
As you can see from the screen-shot, when one or more of the records are selected and the drop button is clicked, I want the records to be deleted from the database but want the corresponding sql insert statements to be written into a file(C:\\xampp\\htdocs\\Lib_auto_project\\Deleted_Records\\delete_log.sql)
.For that to happen I have written the above piece of code. Now the problem is when I select the records and drop them all goes as desired. When I do the same any other instant I want similar sql insert strings, stored as shown above in $result, appended to the end of the file(C:\\xampp\\htdocs\\Lib_auto_project\\Deleted_Records\\delete_log.sql)
. This does not quite happen. Rather the previously written strings get overwritten by the new one. I have tried it over and over but only the recent strings get overwrite the old ones.
It's nice that php says in the manual that a+
(using a
should be just enough for you):
Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
but try to run this code (called test.php
):
<?php
$fp = fopen( 'test.php', 'a+') or die( 'Cannot open!');
echo 'Pos: ' . ftell($fp) ."\n";
echo fgets($fp);
echo 'Pos: ' . ftell($fp) ."\n";
fclose( $fp);
it'll generate this output:
Pos: 0
<?php
Pos: 6
You may either use fseek()
:
fseek( $fp, 0, SEEK_END);
Or use file_put_contents()
with proper parameters:
file_put_contents( $file, $string, FILE_APPEND);
And don't forget to check your file permissions and whether file was opened successfully by code like this:
if( !$fp){
die( 'File cannot be opened!');
}
Try to use file_get_contents and file_put_contents instead of fwrite, and append your queries to array or string:
$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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.