0

A blank row is being inserted before, and along with the actual data row, in my MySQL database. Why is this?

Here is my code:

<?php

define('DB_NAME','my_db_name');
define('DB_USER','my_db_user');
define('DB_PASSWORD','my_db_pass');
define('DB_HOST','localhost');

$link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

if (!$link){
 die('Could no connect'.mysql_error());

}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
 die('Can\'t no connect'. DB_NAME .':'.mysql_error());

}

$name = $_POST['name'];

$sql ="INSERT INTO demo (name) VALUES ('$name')";

if(!mysql_query($sql)){

  die('Error:'.mysql_error());
}

mysql_close();

?>
<html>
<body>
<form  action="demo2.php" method="post">
<input id="name" name="name" type="text" placeholder="" class="form-control input-md" required="">
<input type="submit" class="btn btn-success" value="Add Client">
</form>
</body>
</html>

Here is my MySQL database structure:

Mysql database Structure

Here is the table with a blank entry before the "gunk" name data entry:

enter image description here

I have taken the time to isolate this issue down to one form input with a successful database connection and my data is added, just with blank row before it.

Why am I getting a blank row inserted before new data row in my database?

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
user3741085
  • 99
  • 1
  • 2
  • 12
  • 7
    The above code will only ever insert one row - if you get two rows, then the above code is being called twice. Most likely because you don't bother checking if a form was submitted. So the code runs once for the original GET, and then again for the form submission POST. And note that you're vulnerable to [sql injection attacks](http://bobby-tables.com) – Marc B Aug 31 '16 at 19:47
  • 2
    Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in _mew_ code **a Kitten is strangled somewhere in the world** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Aug 31 '16 at 19:50

4 Answers4

2

Your insert operation is executed on every request, not just when the form is submitted, so the empty row gets inserted into the database when first accessing the page. An easy way to correct this behaviour would be to use the following condition:

if (isset($_POST['name'])) {
    // Your insert command here
}

The above code will work, however you should also include some extra steps in your code, like sanitizing or validating user input.

Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19
1

You're using the same script to display the form and to process the form submission. But when it inserts into the DB, it's not checking whether it's being run in response to the user opening the form for the first time or submitting it. When they go to the URL the first time, $_POST is empty, so you insert a blank value. Add a check for this.

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $link = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);

    if (!$link){
     die('Could no connect'.mysql_error());

    }

    $db_selected = mysql_select_db(DB_NAME, $link);

    if(!$db_selected){
     die('Can\'t no connect'. DB_NAME .':'.mysql_error());

    }

    $name = $_POST['name'];

    $sql ="INSERT INTO demo (name) VALUES ('$name')";

    if(!mysql_query($sql)){

      die('Error:'.mysql_error());
    }

    mysql_close();

}
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

you're doing a MySql query with data that you're not sure if they exist !
inside the <?php?> just do this

if(isset($_POST['name'])){
    if(!empty($_POST['name'])){
        //do your stuff
    }
}

so just make sure that page isn't just loaded (a post request is also coming)
then you make sure that user has inserted something in name input

bobD
  • 1,037
  • 2
  • 9
  • 17
0

You need to refactor your code...on page load the insert query is actually being executed! This inserts a blank row; then on form submission the posted data is inserted. Refactor as:

<?php

    if (!empty($_POST)) {

        // all of your php here

    }

?>
apesa
  • 12,163
  • 6
  • 38
  • 43