3

All this code is written on the same page

 <form method="post" action = "http://mypage.lhosting.info/login/" >
    <p> Username: <input type="text" name = "username" /> </p><br>
    <p> Password: <input type="text" name = "password" /></p> <br>
    <input type="submit" name="submit" value="Register" />
 </form>

 if(isset($_POST['submit']) && !empty($_POST['submit'])) 
 {
   $username = mysqli_real_escape_string($conn, $_POST['username']);
   $password = mysqli_real_escape_string($conn, $_POST['password']);
   $sql = "INSERT INTO Duomenys (Username, Password) VALUES ('$username','$password')"; 
 }

The if(isset($_POST['submit']) && !empty($_POST['submit'])) does not trigger, it sends the account's details to my database everytime I refresh the page. What am I doing wrong?

Note: Still learning PHP and WordPress

8
  • have you put the code of submit in <?php ?> ? Commented May 25, 2017 at 5:22
  • Yes @AhmedGinani Commented May 25, 2017 at 5:22
  • You can check actually $_POST['submit'] != '' instead of !empty Commented May 25, 2017 at 5:27
  • if(isset($_POST['submit']) ) should be enough Commented May 25, 2017 at 6:25
  • When you refresh the page? Do you submit the form and then refresh the page? Does your browser ask you something along the lines of "Are you sure you want to refresh the page and submit the data again?"…? Commented May 25, 2017 at 6:25

1 Answer 1

4

$_POST['submit'] will always set and not empty it's value is already set as Register.

You need to check that username and password should not empty. Hence write

if(!empty($_POST['username']) && !empty($_POST['password'])){
    // querycode here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.