Dear Respected Members/Users,
I Cannot add/insert mysql last insert id in the third table. Kindly please help me out in adding the para_id into the last insert query.
I have demonstrated in the Image file which will help alot in getting my point.
Waiting for response.
thanks. 94fea611ba93443aecd1d5f13b6b82c8

4 Years
Discussion Span
Last Post by khushhappy

On Ln95, you're setting last_id to last_insert_id when last_insert_id is the query for INSERT INTO directives


If I see it right, you have 12 table rows and 11 values in your sqr1.
This will give a error and will insert noting, giving you no last_insert_id


The biggest problem with that code is that - unless it's done in the code you hid between the first two queries - you seem to be executing the queries regardless of what happened with the previous query. If the first query fails, you still execute the second query, and then the third query. You need to check the return value of the queries and make sure they were successful before moving on to the next.

Another kind of catastrophic issue there is that you inject user input directly int the query string. That's a huge security risk. You should read up on SQL Injection.

One more thing that jumps out at me is that when you do chained inserts like that, you usually want them all to succeed, or all to fail. You don't want the first two to succeed, but the third to fail, leaving you with a half finished chain of inserts. - To prevent that, you would normally use transactions. Unfortunately the old MySQL API functions (mysql_query and the rest of that family) are ancient and do not support them. They should no longer be used. You should be using MySQLi or PDO instead. They also support prepared statements, which protects you from the SQL Injection riks.

Edited by Atli


thanks diafol, atli and others for cooperating.
well actually when i echo the values from the query.it works till the sql query2.the sql3 do not works out.well ill gona try for mysqli and pdo but for time being i need to figure it out with the same scenario.
cant we store the initial last_id value in a variable and then re-use accordingly to the need; as in sql3 query, if yes the how ? or any other method ?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.