Jump to content

simple comment form/guest book


jacko310592

Recommended Posts

the follwoing code is the very bsic stage of a comment form/guest book, but im stuck at the moment as i cannot get the file im saving the data to to stop rewriting over the data which is already stored on it.

 

<form method="POST" action="">
<input type="text" name="name" value=""><br/>
<input type="text" name="email" value=""><br/>
<textarea name="message" rows="5" cols="20"></textarea><br/>
<input type="submit">
</form>



<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$txtFile = "comments.txt";
chmod("comments.txt", 0777);
$txtFile = fopen($txtFile, 'w');
$stringData = "{$name}<br/> {$email}<br/> {$message}";
fwrite($txtFile, $stringData);
fclose($txtFile);
?>

 

i found on a site that "chmod("comments.txt", 0777);" is supposed to let php edit the file without over-writing it, but it is just over-writing it, can anyone think why this is happening?

 

thanks guys

Link to comment
https://forums.phpfreaks.com/topic/183478-simple-comment-formguest-book/
Share on other sites

its your opening modifier,

 

you are using $txtFile = fopen($txtFile, 'w');

 

notice the w?

 

fopen() @php.net

'r' Open for reading only; place the file pointer at the beginning of the file.

'r+' Open for reading and writing; place the file pointer at the beginning of the file.

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

'a+' 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.

'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.