Jump to content

Quick fwrite Help


aeonsky

Recommended Posts

Hi!

 

I just had a quick question about fwrite command.

 

Basically what I'm doing is an order page that takes the info from forms and writes them to a txt file, which is the database. The other PHP file reads the info from the txt file and place the info into a table.

 

I've got everything working except a little problem with fwrite. Each time it writes to a text file, it deletes everything (from the database file) and then writes the new info. I want to keep the old info and then add the new info.

 

I've looked around the web to fix the problem, but couldn't find anything!

 

Thank you very much for the time and consideration!  :)

 

P.S.

 

Here's the code (I apologize, for probably writing the code in a horrific manner, I'm new at writing PHP, I can edit it very nicely though) :

 

I need &next because it write a new line (and each new line in the txt file represents a new row in the table) and I need fwrite($fp, "|"); because each | sign represents a new cell in the table.

 

<?PHP
  $next = "
";
  $name = $_REQUEST['name'] ;
  $room = $_REQUEST['room'] ;
  $order = $_REQUEST['order'] ;
  $arrangement = $_REQUEST['arrangement'] ;


// Open the file and erase the contents if any
$fp = fopen("admin/db.txt", "w");

// Write the data to the file
fwrite($fp, $next);
fwrite($fp, $name);
fwrite($fp, "|");
fwrite($fp, $room);
fwrite($fp, "|");
fwrite($fp, $order);
fwrite($fp, "|");
fwrite($fp, $arrangement);



// Close the file
fclose($fp);

?>

 

Link to comment
Share on other sites

Either read the data in the text file to a variable, then write that variable back to the file first, or change:

 

$fp = fopen("admin/db.txt", "w");

 

to:

 

$fp = fopen("admin/db.txt", "a");

 

Notice the change from "w" to "a" as the second parameter to fopen.

 

http://www.php.net/fopen#id3485037 is a table that lists what all of the letters mean.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.