0

Hey guys i am learning php and inserting records into my database.

I am confused as to why on page refresh it keeps adding records into my database before I submit the form?

    <?php

include 'core/functions/connect.php';

// escape variables for security
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$first_name = mysqli_real_escape_string($con, $_POST['first_name']);
$last_name = mysqli_real_escape_string($con, $_POST['last_name']);
$email = mysqli_real_escape_string($con, $_POST['email']);

$sql="INSERT INTO users (username, password, first_name, last_name, email )
VALUES ('$username', '$password', '$first_name', '$last_name', '$email')";


  if(isset($_POST['submit'])) {

}
echo "1 record added";

?>




<form action="" method="post">
    <ul>
        <li>
            Username*:<br>
            <input type="text" name="username">
        </li>
        <li>
            Password*:<br>
            <input type="password" name="password">
        </li>
        <li>
            Password again*:<br>
            <input type="password" name="password_again">        
        </li>
        <li>
            First name*:<br>
            <input type="text" name="first_name">
        </li>   
        <li>
            Last name:<br>
            <input type="text" name="last_name">
        </li> 
        <li>
            Email address*:<br>
            <input type="text" name="email">
        </li>  
        <li>
        <input type="submit" name="submit" value="Register">
        </li>            
        </ul>
</form>

I guess I am missing something here but a pointer would be great so i can understand where I am going wrong?

EDIT: I have tried to resolve the issue by the suggest methods below but yes it has stopped inserting on refresh but not submitting data?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
user3725879
  • 69
  • 1
  • 1
  • 11
  • 3
    You're not testing whether the $_POST values are actually set, so it is doing the INSERT every time you load the page. – WillardSolutions Jun 18 '14 at 16:43
  • 1
    Either seperate your form from PHP, or name your submit button `` then wrap your PHP/SQL inside `if(isset($_POST['submit'])){...}` while checking for empty and/or set fields, using `empty()` and/or `isset()` – Funk Forty Niner Jun 18 '14 at 16:45
  • 2
    possible duplicate of [Avoiding form resubmit in php when pressing f5](http://stackoverflow.com/questions/722547/avoiding-form-resubmit-in-php-when-pressing-f5) – Fabricator Jun 18 '14 at 17:19
  • @Fabricator this isn't a dupe because that script is PDO. I am learning myself to understand why it does both insert data on refresh AND form submission – user3725879 Jun 19 '14 at 08:04
  • @Fred-ii- Does the above demonstrate what you have suggested? It seems to be failing on all accounts now. – user3725879 Jun 19 '14 at 08:18
  • 1
    Place `if(isset($_POST['submit'])) {` on top of `$sql="INSERT INTO...` – Funk Forty Niner Jun 19 '14 at 11:55
  • I have literally just combated tat problem, I have moved the code into a separate file which now only inserts data in when I submit the form and it is submitting the data I have filled in. I am now struggling with hashing passwords into the database now :/ – user3725879 Jun 19 '14 at 12:06

1 Answers1

0

Please check the POST array to see if the form has been submitted. Try doing everything you want after the form submit using:

if(isset($_POST['submit'])) // name of your submit button
    // your processing code
}
SoftSan
  • 2,482
  • 3
  • 23
  • 54
Lyle
  • 419
  • 1
  • 6
  • 26
  • Hi, I have tried to understand what you have advised but seems to be failing on me now. I have updated my code above, are you able to advise where I appear to still be going wrong? – user3725879 Jun 19 '14 at 08:19