-1

I have a problem with my PHP code.
I am trying to make a level create function for a small game project me and another person are working on.
My code works.. but generates a lot of duplicates.
Here's the code: (Don't laugh at me for how vulnerable this is, this will be fixed eventually, THIS IS JUST TEST CODE AND WILL NEVER BE ON A PUBLIC SERVER IN ANY CIRCUMSTANCE, OK?)

$mysqli  = new mysqli("localhost", "Username", "Password", "sys");
$SqlQuery = "INSERT INTO levels (levelname, levelauthor, leveldata)
VALUES(\"" . $_GET["levelName"] . "\", \"" . $_GET["levelAuthor"] . "\", \"" . $_GET["levelData"] . "\");";
$query2 = "SELECT * FROM levels WHERE leveldata = \"" . $_GET["levelData"] . "\";";

//echo "SELECT * FROM levels WHERE leveldata = \"" + $_GET["levelData"] + "\";";

$uresult = $mysqli->query($SqlQuery, MYSQLI_USE_RESULT);
$res2 = $mysqli->query($query2, MYSQLI_USE_RESULT);
if ($uresult) {     
    while ($row = $res2->fetch_assoc()) {
        //This should always work. Lol
        echo "(SUC)|ID:" . $row["levelid"];
    }   
}

After running this code, I expected to just check my database and be able to see the test I wrote, without duplicates. I started the PHP development server and went to:

http://localhost/Create.php?levelName=PHPTest&levelAuthor=Test3&levelData=[snip]

I expected to see something along the lines of "(SUC)|ID:4" (there were 3 entries in the database at the time), but I saw this:

(SUC)|ID:4(SUC)|ID:5(SUC)|ID:6(SUC)|ID:7(SUC)|ID:8(SUC)|ID:9(SUC)|ID:10(SUC)|ID:11

This was unexpected. I thought it was just an error in my code (keep in mind, the last one had a broken ID grabbing system, but worked), and that it would work, but then, I went to check the database, and saw a ton of duplicates with the same data.

Does anyone know how to fix this code?

Epicness
  • 1
  • 1
  • 1
  • Please update the question to the current issue. Please don't use this code. Research SQL injections and parameterize your code. – user3783243 Jul 09 '18 at 01:19
  • Yes, I know this code is really bad. I'll definitely change this before release. – Epicness Jul 09 '18 at 01:21
  • By the way, there is a protection mechanism in place. https://pastebin.com/UZYWvh3W – Epicness Jul 09 '18 at 01:29
  • Note: I was just dumb. For anyone that stumbles across this: please set one of your columns as a UNIQUE column to stop the problem. Of course, delete the duplicates first, or it won't like it. – Epicness Jul 15 '18 at 03:24

1 Answers1

0

Obvious question but autocommit is enabled on database? Do you have some open transaction?

Use this to check open transactions on MySQL.

Djonathan Krause
  • 124
  • 3
  • 14
  • I don't know. If autocommit wasn't enabled, I don't think my previous code revision would've worked. Here's the output of the command, btw: https://pastebin.com/skYX4P0x – Epicness Jul 09 '18 at 01:03
  • Run the result of: select concat('KILL ',id,';') from information_schema.processlist – Djonathan Krause Jul 09 '18 at 11:55