co pro Games co pro Games - 2 months ago 7
PHP Question

Im using POST to add data from form to TXT file but it keeps override the old data

How do i make it save multiple data strings without removing the old ones (in same file) my code is:

<?php

$txt = "joined_teams.txt";
$fh = fopen($txt, 'w+');
if (isset($_POST['teamname']) && isset($_POST['shortname']) && isset($_POST['1steamid']) && isset($_POST['2steamid']) && isset($_POST['3steamid']) && isset($_POST['4steamid']) && isset($_POST['5steamid'])){ // check if both fields are set
$txt=$_POST['teamname'].' - '.$_POST['shortname'].' - '.$_POST['1steamid'].' - '.$_POST['2steamid'].' - '.$_POST['3steamid'].' - '.$_POST['4steamid'].' - '.$_POST['5steamid'];
file_put_contents('joined_teams.txt',$txt."\n",FILE_APPEND); // log to joined_teams.txt
exit(header('location:http://coprogames.com'));
}
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file

?>


Hope you can help me :)

Answer

You are doing the same thing twice. You only need the call to file_put_contents and can remove the fopen/fwrite calls. All you need is this:

if (isset($_POST['teamname']) && isset($_POST['shortname']) && isset($_POST['1steamid']) && isset($_POST['2steamid']) && isset($_POST['3steamid']) && isset($_POST['4steamid']) && isset($_POST['5steamid'])){ // check if both fields are set
   $txt=$_POST['teamname'].' - '.$_POST['shortname'].' - '.$_POST['1steamid'].' - '.$_POST['2steamid'].' - '.$_POST['3steamid'].' - '.$_POST['4steamid'].' - '.$_POST['5steamid']; 
   file_put_contents('joined_teams.txt',$txt."\n",FILE_APPEND); // log to joined_teams.txt 
   exit(header('location:http://coprogames.com'));
}