12

I'm having an issue and was hoping that someone could help me out.

My issue is that whenever I run the code in the "MainFile", It outputs the two ID's that it has added the entries at, but then when I view my database I have six entries instead of two... Could anyone tell me why it might be doing this?

Note: I've only supplied snippets of the code from the classes because supplying the full classes would be way too much code. This is the only code be executed though.

CODE IN EDIT HISTORY

Edit: I've added a debug log to the bottom of this post verifying that I'm only executing the SQL code once for each query.

Edit: I'm no longer using the serialization method as it is clearly a bad idea for storing this type of data. However, even with my new code that is storing each individual value within the database I'm still getting three entries instead of one. So, it's the same issue. Anything?

Edit: After a few days of debugging I have narrowed it down to this line that is causing the issue

Throwlite::$systemSQL->executeSql("INSERT into ".SQL_COMMENTTHREADS_TABLE." (id, sort_order) values (DEFAULT, '2')");

You can view the LiteSQL class here for reference: http://pastebin.com/a4C6fF4u

Also, For reference, Here is the code being used to create the Table:

"CREATE TABLE IF NOT EXISTS `" . SQL_COMMENTTHREADS_TABLE . "` (`id` int unsigned NOT NULL AUTO_INCREMENT, `sort_order` int NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;"

And, Even though I'm pretty sure it shouldn't matter, Here is where SQL_COMMENTTHREADS_TABLE is bing defined.

define( 'SQL_COMMENTTHREADS_TABLE', "tl_comment_threads");
30
  • @shA.t It should be pretty obvious that it is PHP, but I've added the tag just in case. Any help at all?
    – Nathan F.
    Jun 1, 2015 at 4:44
  • I think your problem is not about MySQL behaviors and it is about your PHP code that I can't help you on it ;).
    – shA.t
    Jun 1, 2015 at 4:56
  • 1
    It is possible that a trigger in the database could cause this behavior. Jun 1, 2015 at 14:24
  • 1
    Do you use a MySQL Cluster Version? I had a similar problems once with a locally installed cluster version (cluster version with all nodes on the same machine for testing purposes). Jun 2, 2015 at 18:45
  • 1
    By the way, you are missing a second } on line 51 of the pastebin you posted. The if(sizeof($binds) > 0){ was not closed.
    – Lux
    Jun 7, 2015 at 1:15

5 Answers 5

1

I'm not sure this answers your question, but you seem to be specifying the ID that you are inserting. I'm not sure whether you aren't using a PRIMARY KEY on your id column or why this works at all, but maybe you want this:

INSERT INTO " . SQL_COMMENTS_TABLE . " (id, thread) VALUES (DEFAULT, ?)

9
  • Also, why are you specifying the ID is a string (with the "ss") when it seems to be a number? I assume the rows have AUTO_INCREMENT if you are using GetLastInsertID Jun 3, 2015 at 21:11
  • Honestly, As far as the "SS" part goes, I wasn't thinking when I did it but I hadn't had any issues with it. I should change that though, And now that it's been brought to my attention I will once I'm home from work. As far as the PRIMARY KEY goes, I do have ID set as the primary Key. I'm going to try a few more things out tonight including your proposed solution and I'll let you know how it goes.
    – Nathan F.
    Jun 3, 2015 at 21:35
  • I've made a few edits to my question above. I've narrowed down the source of the issue, I just need to figure out what the issue actually is. If you could look at the question above for the new details and weigh in, that'd be awesome. Thanks.
    – Nathan F.
    Jun 4, 2015 at 1:19
  • I have made a push to the projects GitHub page where you can view all of the code in the project. Please note that the project is largely unfinished. The code you've been viewing can be found under "classes/system/core/sql/litesql.class.php" and "classes/system/comments/". The only file being executed in this test is the "tester.php" file located in the root directory of the project. github.com/thefiscster510/ThrowLite
    – Nathan F.
    Jun 7, 2015 at 2:24
  • After running this on another system, I've realized that is has something to do with my WAMP installation or configuration. I've tried wiping my WAMP installation and reinstalling with no success. Any suggestions?
    – Nathan F.
    Jun 7, 2015 at 19:11
0

When updating UPDATE tl_comments SET thread=? WHERE id = ? and the second ? is not replaced with a valid existing id but instead remains empty, the database can't find an entry to update and inserts a new row instead. Thus creating another entry wich leads you to generating 2 rows instead of just one every time you query.

Which means the GetLastInsertID() function is not working properly. This can be caused by a few things but there are already many helpful hints to that question around at stackoverflow.

10
  • 1
    I will check this out when I get home from work tonight. But, I'm pretty sure I've already verified that I am pulling the right ID with GetLastInsertID(). I'll let you know tonight when I'm home.
    – Nathan F.
    Jun 2, 2015 at 20:00
  • After researching into this, I've verified that the GetLastInsertId() method is working properly. Here is a copy of the complete LiteSQL Class: pastebin.com/a4C6fF4u It is initialized in the main file by doing ThrowLite::$systemSQL = new LiteSQL();
    – Nathan F.
    Jun 2, 2015 at 22:57
  • Raphael, are you sure that LiteSql make INSERT if can't make UPDATE? (thanks, thefiscster510). Jun 3, 2015 at 6:35
  • Try adding OR IGNORE like in UPDATE OR IGNORE ... INSERT OR IGNORE ... Jun 3, 2015 at 13:35
  • There is a very good explanation here Jun 3, 2015 at 13:36
0

I could not locate any logical issue in the code snippet provided by you. However can you make following changes and post the output here :

  1. Add a getter for "prepared_statements" member variable.
  2. After your insert query finished running, print out output of above getter function.

The way this DB access class is written, it is evident there is no way two insert query can exists in single index of the "prepared_statements" array. Still it is better to check and confirm. Also if you can share the function (under which insert query piece of code is running) and the code snippet from where this function is called can be useful for others to debug the problem.

2
  • I have made a push to the projects GitHub page where you can view all of the code in the project. Please note that the project is largely unfinished. The code you've been viewing can be found under "classes/system/core/sql/litesql.class.php" and "classes/system/comments/". The only file being executed in this test is the "tester.php" file located in the root directory of the project. github.com/thefiscster510/ThrowLite
    – Nathan F.
    Jun 7, 2015 at 2:24
  • After running this on another system, I've realized that is has something to do with my WAMP installation or configuration. I've tried wiping my WAMP installation and reinstalling with no success. Any suggestions?
    – Nathan F.
    Jun 7, 2015 at 19:10
0

I feel somewhere in your code a loop is running which is causing inserting it multiple times in the database. Can you try echo your query so that u get to know how many times your query is running.

Another hack can be put condition to check whether row already present- do not insert.

Can you please post your code so that I can debug it.

6
  • Tonight when I am home from work I will make a push to the GitHub repository so that the entire code library is available. It's rather large, but you can then sift through it. I've posted (In the edit history as well as in the question itself) all of the code that I believe is relevant. However, I will post the rest tonight.
    – Nathan F.
    Jun 5, 2015 at 16:41
  • I have made a push to the projects GitHub page where you can view all of the code in the project. Please note that the project is largely unfinished. The code you've been viewing can be found under "classes/system/core/sql/litesql.class.php" and "classes/system/comments/". The only file being executed in this test is the "tester.php" file located in the root directory of the project. github.com/thefiscster510/ThrowLite
    – Nathan F.
    Jun 7, 2015 at 2:23
  • After running this on another system, I've realized that is has something to do with my WAMP installation or configuration. I've tried wiping my WAMP installation and reinstalling with no success. Any suggestions?
    – Nathan F.
    Jun 7, 2015 at 19:10
  • I just tried uninstalling WAMP and installing XAMPP instead, so as far as I know that means it should be running on a completely clean configuration. However, I'm still getting the same issue where three entries are getting inserted instead of just one. This makes absolutely no sense to me since I ran this exact same code on two other machines running clean installations of WAMP and it only inserted one entry..
    – Nathan F.
    Jun 7, 2015 at 20:05
  • In your /classes/system/comments/core/commentsystem.class.php You are using Throwlite::$systemSQL->executeSql("INSERT into ".SQL_COMMENTTHREADS_TABLE." (id, sort_order) values (DEFAULT, '2')"); so where you have defined $systemSQL object ? you can directly call your executeSql function ? can you please try also can you print your bind array ? Jun 8, 2015 at 7:28
0

By the way, you are missing a second } on line 51 of the pastebin you posted. The if(sizeof($binds) > 0){ was not closed.

5
  • I'm so sorry about this, but after leaving my comment and trying a few more times I realized it's still having the issue. However, it shows one entry, and then I reload phpMyAdmin and the other two are magically there..
    – Nathan F.
    Jun 7, 2015 at 1:45
  • Sorry I can't be of more use.
    – Lux
    Jun 7, 2015 at 1:47
  • I have made a push to the projects GitHub page where you can view all of the code in the project. Please note that the project is largely unfinished. The code you've been viewing can be found under "classes/system/core/sql/litesql.class.php" and "classes/system/comments/". The only file being executed in this test is the "tester.php" file located in the root directory of the project. github.com/thefiscster510/ThrowLite
    – Nathan F.
    Jun 7, 2015 at 2:23
  • After running this on another system, I've realized that is has something to do with my WAMP installation or configuration. I've tried wiping my WAMP installation and reinstalling with no success. Any suggestions?
    – Nathan F.
    Jun 7, 2015 at 19:10
  • I just tried uninstalling WAMP and installing XAMPP instead, so as far as I know that means it should be running on a completely clean configuration. However, I'm still getting the same issue where three entries are getting inserted instead of just one. This makes absolutely no sense to me since I ran this exact same code on two other machines running clean installations of WAMP and it only inserted one entry..
    – Nathan F.
    Jun 7, 2015 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.