3

I'm trying to use this code from this site which inserts empty fields into a database when the page is initially loaded and when the form's fields are filled out and submitted, then another batch of data is inserted into the database (MySQL). How this behavior can be avoided?

<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>

</body>
</html> 
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "1 record added";

mysqli_close($con);
?> 
1
  • when page load, code run. code inserts values even when empty - solution, check form submitted before insert Commented Feb 24, 2014 at 0:59

3 Answers 3

3
<html>
<body>

<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname">
Lastname: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit" name="submit" value="submit" />
</form>

</body>
</html> 
<?php
if($_POST['submit']){
  $con=mysqli_connect("example.com","peter","abc123","my_db");
  // Check connection
  if (mysqli_connect_errno())
    {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $sql="INSERT INTO Persons (FirstName, LastName, Age)
      VALUES
      ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

    if (!mysqli_query($con,$sql))
    {
      die('Error: ' . mysqli_error($con));
    }
      echo "1 record added";

  mysqli_close($con);
}
?> 

You have to check if($_POST['submit']) variable has a value set.

also you need to add some value and a name to your input type="submit" html element

<input type="submit" name="submit" value="Submit!" />
Sign up to request clarification or add additional context in comments.

4 Comments

Also might not suggest a junior to input $_POST data directly into the database #justsaying
if($_POST['submit']) is not the way to check if an array element exists or not. PHP will complain. See Fabio's answer which used isset().
@mickmackusa ofc you can add all sorts of error handling and over bloated frameworks with validation DTOs etc but that was not the question here.
Let's not ramp into ridiculousness. All I am saying is that one condition is not covering an obvious possible scenario. You can waste time arguing, or you can edit your answer to implement a very sensible check. Then these comments can be removed. I use old pages to close new pages -- that is why I need old answers to be of high quality advice. Otherwise, the whole page cannot be used as a dupe target.
2

You need to check for isset() of your submit button before inserting otherwise it will insert an empty row at each load, so add it

</body>
</html> 
<?php

if(isset($_POST['submit']))
{
     $con=mysqli_connect("example.com","peter","abc123","my_db");

     //all your code here
}

Note that your submit button is missing name so change it to

<input type"submit" name="submit">

Comments

1

Put your insert code jn an if statement like:

If (isset($_post)) { Code here }

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.