-2

The following code is supposed to insert values into a database:

$sql = "INSERT INTO savedtimes (username, session, value3, value4, value5)
          VALUES ('$user', '$q', '$value3', '$value4', '$value5')";
  mysqli_query($db, $sql);

It works, but the only problem is that the row is getting inserted twice (every time!).

So, for example, I am inseting the values 'myUsername', '1', 'foo', 'bar', 'fubar', the values get stored in the databse like:

| id | username | session | value3 | value4 | value 5|

| 1 |myUsername| 1 | foo | bar | fubar |

| 2 |myUsername| 1 | foo | bar | fubar |

I also tried changing the browser, but that has no effect. I noticed that there was another question with the same problem, but no answer. Can somebody please help me?

Edit:

I have added some more of my code as requested by some of you below:

session_start();
$q = $_GET['q'];
$user = $_SESSION['username'];
$db = mysqli_connect('localhost', 'username', 'password', 'databaseName');
$sql = "SELECT * FROM savedtimes WHERE username = '$user' AND session = '$q'";
$result = mysqli_query($db, $sql);
if (mysqli_num_rows($result) == 1) {
  $sql2 = "SELECT time FROM savedtimes WHERE username = '$user' AND session = '$q'";
  $updateRequired = mysqli_query($db, $sql2);
  if ($updateRequired == "-:--.---") {
    $query = "UPDATE savedtimes
              SET value3 = '$value3', value4 = '$value5'
            WHERE session = '$q'";
    mysqli_query($db, $query);
  }
  else {
    $insert = true;
  }
}
if ((mysqli_num_rows($result) > 1) || ($insert == true)) {
  $sql = "INSERT INTO savedtimes (username, session, value3, value4, value5)
          VALUES ('$user', '$q', '$value3', '$value4', '$value5')";
  mysqli_query($db, $sql);
}
aravk33
  • 469
  • 2
  • 10
  • 18
  • The rest of your script is needed. – bassxzero Sep 20 '17 at 16:23
  • 1
    are u sure are u not calling the mysqli_query twice? post the rest of the code – Masivuye Cokile Sep 20 '17 at 16:23
  • 1
    Can you show some more context of the code where this query is being executed? This problem seems unlikely unless it's running in a loop. – Don't Panic Sep 20 '17 at 16:24
  • 3
    If you ever publish your site the first thing anyone would do is register with username `Robert';) DROP TABLE savedtimes; --` .. Call them [Bobby Tables](https://xkcd.com/327/) – apokryfos Sep 20 '17 at 16:25
  • If I post the rest of the code, it's huge, but I'm pretty sure it's this, as I did ctrl+F "INSERT INTO", and only 1 result came up. But still, I'll try to edit and post some more of my code. – aravk33 Sep 20 '17 at 16:25
  • How is this code being called? – apokryfos Sep 20 '17 at 16:26
  • 1
    please check if there is some tirgger defined on this table... – prabhat mishra Sep 20 '17 at 16:28
  • [Little Bobby](http://bobby-tables.com/) says ***[your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)*** Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Sep 20 '17 at 16:29
  • There's something you're not showing/tellling us here, as in "details". If the page is reloaded, or something else. – Funk Forty Niner Sep 20 '17 at 16:31
  • I have now include much more of my code. Also, this is not the final version, I will use prepared statements in future versions of this, or sooner if possible. – aravk33 Sep 20 '17 at 16:37
  • 3
    Add an `exit;` after `mysqli_query($db, $sql);` does it still add duplicate records? Do @ me back – Funk Forty Niner Sep 20 '17 at 16:38
  • @Fred-ii- Thank you so much, I tried it and I think it works, but I still need to check it properly in my full code. But so far, yours is the most useful solution I've got. – aravk33 Sep 20 '17 at 16:45
  • @Cubetastic I checked the updated code. It appears to be alright. could you echo something instead of the insert query and see if that is getting called twice? – pro_cheats Sep 20 '17 at 16:46
  • 1
    @pro_cheats I already tried that, but it is NOT getting called twice! That's when I decided I can't figure it out on my own, and had to post it here. Thanks for trying to help me anyway! – aravk33 Sep 20 '17 at 16:47
  • @Cubetastic well, that's a step in the right direction. – Funk Forty Niner Sep 20 '17 at 16:48
  • check the page call too... – prabhat mishra Sep 20 '17 at 16:49
  • @prabhatmishra What do you mean "page call"? – aravk33 Sep 20 '17 at 16:50
  • @Fred-ii- Thank you, `exit;` is working, but still, I can't figure out why this happened. Can somebody please explain that? – aravk33 Sep 21 '17 at 01:40
  • all I can think of is that something might be reloading somewhere and the session is still alive; kind of a best guess – Funk Forty Niner Sep 21 '17 at 01:55
  • @Fred-ii- I'm using AJAX to call the page, if that makes any difference. – aravk33 Sep 21 '17 at 02:06
  • It could make a diference, depending on how it's used, if it's used as a live search. You would need to place everything or the INSERT into a conditional statement and that could vary upon what you want to run first. A live search including an initial page load could also trigger your code to execute twice and doing the double insert. A hack for this would be to ALTER your column(s) to have a UNIQUE constraint. There are too many possible solutions for this, given what I've suggested earlier/already. – Funk Forty Niner Sep 21 '17 at 12:07

1 Answers1

-2
$sql = "INSERT INTO savedtimes (username, session, value3, value4, value5) VALUES";
for($i=1;$i<=1;$i++){
$sql .="('$user', '$q', '$value3', '$value4', '$value5')";
}
mysqli_query($db, $sql);

Try this code

Safan
  • 75
  • 10
  • Turn the tide against teaching/propagating sloppy and dangerous coding practices. If you post an answer without prepared statements [you may want to consider this before posting](http://meta.stackoverflow.com/q/344703/). Additionally [a more valuable answer comes from showing the OP the right method](https://meta.stackoverflow.com/a/290789/1011527). – Jay Blanchard Sep 20 '17 at 16:29
  • 3
    Do or do not, there is no "try". A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Sep 20 '17 at 16:30