1

I'm quite new to PHP and an absolute beginner when it comes to SQL. I'm just learning the basics and I can't get my head around why my code is generating a duplicate entry every time the form is submitted, e.g.

Name: Joe Blogs Email: [email protected]
Name: Joe Blogs Email: [email protected]

The database has a table called user and two columns, name and email.

My index file looks like this, it has a simple form for name and email, and inserts the data on submit:

<form method="post" action="insert.php">
    <input name="name" type="text">
    <input name="email" type="email">
    <input type="submit" value="Submit Form">
</form>

<?php
$servername = "localhost";
$username = "DB_USER";
$password = "PASSWORD";
$dbname = "DB_NAME";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sqlout = "SELECT name, email FROM user";
$result = $conn->query($sqlout);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<b>Name:</b> " . $row["name"]. " <b>Email:</b> " . $row["email"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>

<form method="post" action="wipe.php">
    <input type="submit" value="Wipe ALL Data">
</form>

This insert.php file is called when the form is submitted:

<?php
$servername = "localhost";
$username = "DB_USER";
$password = "PASSWORD";
$dbname = "DB_NAME";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO user ( name, email ) VALUES ( '{$conn->real_escape_string($_POST['name'])}', '{$conn->real_escape_string($_POST['email'])}' )";
$insert = $conn->query($sql);

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

<a href="index.php">Back</a>

I've probably made some basic mistakes but I'm not sure why it is adding duplicates. Is it something to do with connecting twice to the database in each file? Is there a better way to connect only once? Or is it caused by the form submission itself?

3
  • 1
    I have seen a few of these call it twice errors recently here. What tutorial are you following because it is obviously one we should recommend people to avoid Commented Oct 23, 2015 at 10:30
  • Mark the best answer to help others Commented Oct 23, 2015 at 10:40
  • This wasn't a specific tutorial. I started with the w3schools tutorial on the basics of inserting data but I moved on to adding a form and using that to insert the data when submitted. I missed the double query call because I'm so new to SQL and PHP. Commented Oct 23, 2015 at 10:41

4 Answers 4

5

Because you call query twice:

$insert = $conn->query($sql);

if ($conn->query($sql) === TRUE) {

You should rewrite is as

$insert = $conn->query($sql);

if ($insert === TRUE) {

Also, you should really be using prepared statements.

Sign up to request clarification or add additional context in comments.

Comments

1

Your code Call $conn->query twice

$insert = $conn->query($sql);// first time 

if ($conn->query($sql) === TRUE) {// second time

Comments

1
if ($insert === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

Comments

0

You need change:

$sql = "INSERT INTO user ( name, email ) VALUES ( '{$conn->real_escape_string($_POST['name'])}', '{$conn->real_escape_string($_POST['email'])}' )";
$insert = $conn->query($sql);

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

to

$sql = "INSERT INTO user ( name, email ) VALUES ( '{$conn->real_escape_string($_POST['name'])}', '{$conn->real_escape_string($_POST['email'])}' )";
$status = $conn->query($sql);

if ($status === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

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.