0

How do I explain this....

Basically say if I create a post, I click submit, the following database insert would happen

$sql="INSERT INTO comments (userid, topicid, category, topicname, comment, date, name, avatar)
 VALUES
 ('$row[id]','$content[id]','blogs','$content[topic]','$_POST[comment]','$today','$row[name]', '$row[cavatar]')";

now say at the same time of that post I want another INSERT query running, how do I get the AUTO created ID number from the first query into the second query?

second query would be

$notifyme="INSERT INTO notifyme (userid, catid, category, date,time)
 VALUES
 ('$row[id]', ,'comments','$date','$time')";

they both run the same time but I need the AUTO created id number off QUERY 1 to insert under CATID for QUERY 2

$sql="INSERT INTO comments (userid, topicid, category, topicname, comment, date, name, avatar)
 VALUES
 ('$row[id]','$content[id]','blogs','$content[topic]','$_POST[comment]','$today','$row[name]','$row[cavatar]')";

$notifyme="INSERT INTO notifyme (userid, catid, category, date,time)
 VALUES
 ('$row[id]', ,'comments','$date','$time')";

is how it looks when ran, both will be running the same time

Lonely Ranger
  • 89
  • 1
  • 10
  • 3
    `last_insert_id()`, and you are vulnerable to [sql injection attacks](http://bobby-tables.com). – Marc B Jun 02 '15 at 20:19
  • See a duplicate http://stackoverflow.com/questions/17112852/get-the-new-record-primary-key-id-from-mysql-insert-query – Dmitry Sadakov Jun 02 '15 at 20:20
  • what Marc said, or another method I use is to do a query select, run a while loop, assign a variable to an `id` row, and add `+1` to it. I use it often. But Marc's suggestion is better. – Funk Forty Niner Jun 02 '15 at 20:21
  • [You're someone's "mentor"...](http://stackoverflow.com/a/30605835/) there @MarcB – Funk Forty Niner Jun 02 '15 at 20:24
  • if @MarcB could post this as an answer I will give the answer credits to him :) – Lonely Ranger Jun 02 '15 at 20:30
  • @LonelyRanger He can't; he closed the question. – Funk Forty Niner Jun 02 '15 at 20:33
  • darn well it was very handy as I had no clue how to phrase my question or if it made any sense lol – Lonely Ranger Jun 02 '15 at 20:34
  • @LonelyRanger which MySQL API are you using to connect with? `mysql_`, or `mysqli_` or PDO? – Funk Forty Niner Jun 02 '15 at 20:35
  • then http://php.net/manual/en/mysqli.insert-id.php is the one to use. I've outlined a few comments in an answer given below. They just need to adjust their answer in regards to that function. @LonelyRanger should you want to just basically accept it; the choice is yours, but again... they need to adjust their answer with the correct function for the matching MySQL API. – Funk Forty Niner Jun 02 '15 at 20:38
  • @LonelyRanger I've decided to modify and improve the answer given below, should you want to accept it and close the question. – Funk Forty Niner Jun 02 '15 at 20:50

1 Answers1

0

Since you said in comments that you are using the mysqli_ API to connect with, use mysqli_insert_id() for retrieving a record id after each query ran.

Reference:


Sidenote:

Your present code is open to SQL injection. Use prepared statements, or PDO with prepared statements, they're much safer.

Community
  • 1
  • 1
Alex
  • 715
  • 5
  • 14