Jump to content

why writes three times


rysio

Recommended Posts

i'm using the fwrite() function of the tutorial kind... looks like this: after some (nooby) modifications:
[code]<?php


$filename = 'comments.txt';

$handle = fopen($filename, "a");

$somecontent = "$comment\n257";

fwrite($handle, $somecontent);

fclose($handle);

?> [/code]

the $comment data comes from another script which is placed in the same file... this works fine, but the "257" fragment (being my way of marking which file the comments are from...)gives the bug - the script writes the number 3 times... what could be done about it?
Link to comment
Share on other sites

there - the whole lot:

[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-2">
<title>257</title>
<link rel="stylesheet" type="text/css" href="http://www.tkk.pl/~kupka27/styl.css">

</head>


<body>
            <?php
foreach($_POST as $k=>$v)
$$k = get_magic_quotes_gpc() ? $v : addslashes($v);

$userIP = $_SERVER['REMOTE_ADDR'];

$txt=fopen("test.gb","r");  // zapis informacj



$gbfile="";
while($line=fgets($txt,3000))
$gbfile.=$line;

//name
if(isset($name))
if($name==""){
$errorfarbe_1 = "red";
$error = "error";
}
else{
$errorfarbe_1 = "black";
}

//comment
if(isset($comment))
if($comment==""){
$errorfarbe_2 = "red";
$error = "error";
}
else{
$errorfarbe_2 = "black";
}

if(!$aktion){
$aktion = "aktion";
}

if($error=="error"||$aktion=="aktion"){
?>

    </span>
    <div align="center"><span class="style1"><span style="font-variant: small-caps">skargi i wnioski</span>
      <?
              echo "<p>$gbfile</p>"
              ?>
  </span>
            </div>
            <form action="<? echo $PHP_SELF;?>" method="post">
              <div align="center"><br />
            nick: <input name="name" type="text" class="wpisz" size="32"
            <? if(isset($name)) echo "value='$name'";?>>
                <br />
              <br />
            e-mail: <input name="mail" type="text" class="wpisz" size="30">
            <br />
              <br />
            strona: <input name="site" type="text" class="wpisz" value="http://" size="30">
            <br />
            <br>
              twój komentarz:<br />
            <textarea name="comment" cols="30" rows="5" class="prawa"><? if(isset($comment)) echo "$comment";?></textarea>
            <br />
            <br />
            <input name="Button" type="submit" class="zapisz" value="zapisz"><br>
    <input type="Hidden" name="aktion" value="senden" class="aktion">
            <br>

  <input type="hidden" name="userIP" class="aktion"
          value="<? $userIP; ?>"><br>

            </div>
          </form>
  <span class="style2">

                <?        }else{

if(isset($name)&&isset($comment)&&isset($mail)&&isset($site)&&isset($userIP))
{
$name=stripslashes(strip_tags($name));
$comment=stripslashes(strip_tags($comment));
$mail=stripslashes(strip_tags($mail));
$site=stripslashes(strip_tags($site));
$userIP=stripslashes(strip_tags($userIP));


$gbfile="<div align=center><br>\n$comment<br><a href='mailto:$mail'><b>$name</b></a> (ip:$userIP)\n"."\n<text>/<a href='$site' target='blank'>#</a><text>/</text>\n "."\n
".date("d.m.y H:i:s")." "."</div>\n".$gbfile;
fclose($txt);
$txt=fopen("test.gb","w");
fputs($txt,$gbfile);
}


echo "<p align=center><a href='test.php'><b>powrót</b></a><br><br>" . $gbfile . "</p>";
fclose($txt);
}

?>

<?php


$filename = 'comments.txt';

$handle = fopen($filename, "a");

$somecontent = "$comment\n257";

fwrite($handle, $somecontent);

fclose($handle);

?>

         
    </span>
      </td>
</tr>
    </table> </td>
  </tr>
</table>
</body>
</html>[/code]
Link to comment
Share on other sites

Looks like it would write to the file regardless of any user input

This bit may need an isset() around it:
[code]
<?php
if(isset($_POST['somevar']) {
  $filename = 'comments.txt';
  $handle = fopen($filename, "a");
  $somecontent = "$comment\n257";
  fwrite($handle, $somecontent);
  fclose($handle);
}
?>
[/code]

Also you are going to come unstuck using $posted_var instead of $_POST['posted_var']

;)
Link to comment
Share on other sites

when i changed the script to what you suggested, the browser doesn't open it - gives me a white page - nothing.
p.s. i am not sure what i should do with this last piece of advice :"Also you are going to come unstuck using $posted_var instead of $_POST['posted_var']"
am a real noob, so, please - if you can - do explain just roughly, in short, what these commands do. i did try to read the turorials, but, well... i don't get them fully. in fact that is why i am asking my questions here.
Link to comment
Share on other sites

If you are submitting data from a form, trying to read a variable with $var is dependant on having register_globals set to on, which in later versions of PHP has been disabled by default. The best way to read posted vars is to use $_POST['var']. Also, use $_GET['var'] for query strings passed in the URL.

Re your original post, if your code is not working, post what you have after making your changes....

:)
Link to comment
Share on other sites

ok, now seriously - first - my english is a second language, so i do not fully understand everything. secondly - all that programming jargon does make it more difficult. on top of that i am not that well acquainted with the php itself, so please do treat me like a blonde bimbo - tell me what to put and where - please.
Link to comment
Share on other sites

[quote author=rysio link=topic=122719.msg508193#msg508193 date=1169161500]...so please do treat me like a blonde bimbo - tell me what to put and where - please.[/quote]
Most blonde bimbos don't need to be told what to put and where. They've usually had it put just about anywhere it can go. Thanks for that - it made my day.
Link to comment
Share on other sites

[quote author=mjdamato link=topic=122719.msg508919#msg508919 date=1169252797]
[quote author=rysio link=topic=122719.msg508193#msg508193 date=1169161500]...so please do treat me like a blonde bimbo - tell me what to put and where - please.[/quote]
Most blonde bimbos don't need to be told what to put and where. They've usually had it put just about anywhere it can go. Thanks for that - it made my day.
[/quote]
well - you are absolutely right - it IS totally hilarious.. didn't notice it while writing though ;)
Link to comment
Share on other sites

[quote author=mjdamato link=topic=122719.msg508919#msg508919 date=1169252797]
Most blonde bimbos don't need to be told what to put and where. They've usually had it put just about anywhere it can go. Thanks for that - it made my day.
[/quote]


bahahahahahaha. Thats fantastic.
Link to comment
Share on other sites

[code]if(isset($comment)){
  $number= 257;
  $filename = 'comments.txt';
  $handle = fopen($filename, "a");
  $somecontent = "\n photo number: $number\n comment: $comment\n added by: $name\n posted on: ".date("d.m.y H:i:s")."\n";
  fwrite($handle, $somecontent);
  fclose($handle);
}
[/code]
ok here it is: i've done some changes, and know already why it used to write the data three times.
the thing is - every time the file was executed by a browser (refreshed, opened etc.) the string [sub]$somecontent = "$comment $number ".date("d.m.y H:i:s")."\n";[/sub] is written into the comments.txt file
but i only wanted the script to write any content into the comments.txt file when the comment field in the form was filled. actually by this last sentence i've come to the sullution. as simple as the if function - by the way - just learned it because i so much wanted to make this work! and it does :)

well - this one is finally solved!
did it myself, but thanks phpfreaks for everything!
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.