NoxxerTV
NoxxerTV

Reputation: 5

Form to text file

I have setup a form to allow people to submit accounts to my account generator but I have a couple problems:

  1. Old entries get deleted when new one is made

  2. Entries are displayed 2 times in the file

below I have included my code please help me

<?php
$file = fopen("collections/mc.txt", "w") or die("Unable to open file!");
if (isset($_POST['nfa'])) {
    $type = "nfa";
}

if (isset ($_POST['sfa'])) {
    $type = "sfa";
}

if (isset ($_POST['ufa'])) {
    $type = "ufa";
}

if (isset ($_POST['unknown'])) {
    $type = "unknown";
}

$username = $_POST['email'];
$password = $_POST['password'];
$mailurl = $_POST['accessurl'];
$mailpassword= $_POST['accesspassword'];

$details = "
==================================
type: $type
email: $username
password: $password 
mail url: $mailurl 
mail password: $mailpassword
==================================
";
fwrite($file, $details);
$details = "
==================================
type: $type
email: $username
password: $password 
mail url: $mailurl 
mail password: $mailpassword
==================================
";
fwrite($file, $details);
fclose($file);
?>

<a href="/mc.php">Go Back</a>

Upvotes: 0

Views: 37

Answers (1)

Chris Read
Chris Read

Reputation: 136

  1. Try using file_put_contents with the FILE_APPEND flag.
  2. You have duplicated the code that writes to the file, so it will write twice.

This should work:

<?php

if (isset($_POST['nfa'])) {
    $type = "nfa";
}

if (isset ($_POST['sfa'])) {
    $type = "sfa";
}

if (isset ($_POST['ufa'])) {
    $type = "ufa";
}

if (isset ($_POST['unknown'])) {
    $type = "unknown";
}

$username = $_POST['email'];
$password = $_POST['password'];
$mailurl = $_POST['accessurl'];
$mailpassword= $_POST['accesspassword'];

$details = "
==================================
type: $type
email: $username
password: $password 
mail url: $mailurl 
mail password: $mailpassword
==================================
";

file_put_contents("collections/mc.txt", $details, FILE_APPEND);

?>

<a href="/mc.php">Go Back</a>

Upvotes: 1

Related Questions