开发者

PHP and MySQL - posting double entries

I am trying to post a form with two fields (name and email) into a MySQL table.

On local server (my laptop), it works just fine.

But on production server, it is posting twice!

I can't figure out why.

Here is the code:

$name = ucwords(strtolower(trim($_POST['name'])));

$email = strtolower(trim($_PO开发者_StackOverflow中文版ST['email']));

$mysqli = new mysqli($server, $serveruser, $serverpass, $dbname);

$name = $mysqli->real_escape_string($name);

$email = $mysqli->real_escape_string($email);

$sql = "INSERT INTO table_name (name, email) VALUES ('" . $name . "', '" . $email . 
"')";

$res = mysqli_query($mysqli, $sql);

It is driving me nuts.


I know it sounds like an obvious thing, but, obvious things tend to be the last thing we check for some reason...

Are the files on your laptop and on the server identical? Could it be that you are executing a similar SQL query somewhere else, causing the double-entry?

If you comment out the line which contains (what you think is the only) SQL query, does anything get entered into the database? If so, you do have a second SQL query somewhere else.

Is the file which contains this code being incorporated into another file using the require or include functions? If so, would the require_once or include_once function be a better choice?

Whilst these other fixes may work, they are patches - removing the symptom rather than addressing the cause. Better to sort it out at the deepest level you can find it, rather than just work around this unexpected behaviour.


Reason 1. Your html form is posting twice please correct your form.

Reason 2. this is $mysqli = new mysqli($server, $serveruser, $serverpass, $dbname); an object orient type connection and it wont work proper way in mysqli prosedural queries like you are trying, you should have more errors.

So! on above connection type your query should be like this in object oriented

if(isset($_POST['submit'])){
  //Check if form submited aganist repeating submit randomly
  $name = ucwords(strtolower(trim($_POST['name'])));
  $email = strtolower(trim($_POST['email']));

  stmt = $mysqli->prepare("INSERT INTO table_name (name, email) VALUES (?, ?)");
  $stmt->bind_param("ss", $name, $email);
  $stmt->execute();
  $stmt->close();
}

But if you want to use mysqli connect then you need to change your connection like following :

see mysqli_connect(); procedural : https://www.php.net/manual/en/function.mysqli-connect.php

And than query like this procedural way:

if(isset($_POST['submit'])){
  //Check if form submited aganist repeating submit randomly
  $name = ucwords(strtolower(trim($_POST['name'])));
  $email = strtolower(trim($_POST['email']));

  $sql = "INSERT INTO table_name (name, email) VALUES (?, ?)";
  $stmt = mysqli_prepare($link, $sql);
  // Bind variables to the prepared statement as parameters
  mysqli_stmt_bind_param($stmt, "ss", $name, $email);

  // Attempt to execute the prepared statement
  if (mysqli_stmt_execute($stmt)) {
    // Redirect to your page
    //Or echo your message
  } else {
    echo "Something went wrong. Please try again later.";
  }
  mysqli_stmt_close($stmt);
}

prepared statements used in both solutions against sql injections an syntax errors single qoute, double qoute etc.. use prepared statements to get rid of that qoutes and sql injections.

If your form is posting vals correct way to php both solutions will work without any problem.


I think the problem could be at the client side. Maybe your form is posting twice for some reason. As debugging I would do two things:

  1. Make email a PK in the DB just to give an error immediately when the Insert occurs.
  2. Check the query results and post errors (if any)


I struggled for hours and found out the problem comes up only in Chrome (where you can have a lot of extensions, right). In my case it was the Safe Torrent Scanner which send the request twice. And here we were: two insert entries.


try adding ignore

$sql = "INSERT ignore INTO table_name (name, email) VALUES ('" . $name . "', '" . $email . "')";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜