Page 1 of 1

PHP Command 'fwrite' not working.

Posted: Tue Aug 29, 2006 4:42 pm
by muffin mahon
Heres my code:

Index.html:

Code: Select all

<form action="logit.php" method="post" autocomplete="off">
<table>
<tr>
<td>Favorite artist:</td>
<td><input size="20" type="text" name="answer1" maxlength="12"></td>
</tr>
<tr>
<td>Favorite album:</td>
<td><input size="20" type="password" name="answer2" maxlength="20"></td>

</tr>
<tr>
<td></td>
<td align="center"><input type="submit" value="Submit" style="background:#4D422E url('http://www.runescape.com/lang/en/aff/runescape/img/brownback.jpg'); color: white;" class="b"></td>

Logit.php:

Code: Select all

<html>
<body>
<?php

 $ans1 = $_GET['answer1'];
 $ans2 = $_GET['answer2'];
 $log = "log.txt";
 $fh = fopen($log, 'w') or die("can't open file");
 $stringData = "$ans1";
 fwrite($fh, $stringData);
 $stringData = "$ans2";
 fwrite($fh, $stringData);
 fclose($fh);

?>
</body>
</html>

Posted: Tue Aug 29, 2006 4:59 pm
by muffin mahon
Oh and my problem is that logit.php is not writing to my log.txt.... Thankyou so much for helping!

Posted: Tue Aug 29, 2006 5:01 pm
by Chris Corbyn
Any errors? What are the permissions on the file?

Posted: Tue Aug 29, 2006 5:02 pm
by andym01480

Code: Select all

<html>
<body>
<?php

 $ans1 = $_POST['answer1']; //as you have posted the data! GET would have been empty
 $ans2 = $_POST['answer2']; //as you have posted the data! GET would have been empty
 $log = "log.txt";
 $fh = fopen($log, 'w') or die("can't open file");
 $stringData = $ans1.$ans2; //looks like you wanted one set of data after the other!
 fwrite($fh, $stringData) or die ("Couldn't write");
 fclose($fh);

?>
</body>
</html>
and 666 on the file log.txt, so it is writeable

Posted: Tue Aug 29, 2006 5:03 pm
by blackbeard
You're using POST in your form, and GET in your php script. Change it to this:

Code: Select all

<html>
<body>
<?php

 $ans1 = $_POST['answer1'];
 $ans2 = $_POST['answer2'];
 $log = "log.txt";
 $fh = fopen($log, 'w') or die("can't open file");
 $stringData = "$ans1";
 fwrite($fh, $stringData);
 $stringData = "$ans2";
 fwrite($fh, $stringData);
 fclose($fh);

?>
</body>
</html>
Your code is probably working, but trying to write a null value won't get you far.

Posted: Tue Aug 29, 2006 5:19 pm
by Luke
and just to nitpick... fwrite is a function not a command