-1

Here is the query I tried,

$result = "INSERT INTO task_assign(assigned_to,assigned_by,end_on,task_title,task_desc,priority,task_status,history_assigned) VALUES('$assignTo','$assignBy','$endOn','$title','$description','$priority','$status','$assignTo')";

mysql_query("INSERT INTO notification(title,description,priority,category_id,assignment_type,sender_id,receiver_id,date_time)
    VALUES('$title', '$description', '$priority', '$category_id', '1', $assigassignTonBy, $assignTo, NOW())
    ");
    //this return only task_assign
    echo $rowId = mysql_insert_id(); 

How can I get the last insert id from the table?

Anand G
  • 3,130
  • 1
  • 22
  • 28
kalaivanan
  • 63
  • 8

2 Answers2

0

1) Make sure there is column which is 'AUTO INCREMENT' with 'PRIMARY KEY'.

2) mysql_query has been deprecated. You can use object oriented approach PDO or Prepared statement, Mysqli. You can check here too.

3) You can use mysqli_insert_id() OR PDO::lastInsertId().

Ashish Tiwari
  • 1,919
  • 1
  • 14
  • 26
0

Here are a few ideas to try.

  1. Run the first INSERT, then immediately following that call mysql_insert_id() to retrieve the task_assign ID. Then follow the same steps for the next query. Run the query and then immediately call the mysql_insert_id() method. If you have AUTO_INCREMENT set in each table you should get the IDs which can be assigned to separate variables.

    Resource: http://php.net/manual/en/function.mysql-insert-id.php

    Be sure to review the Return Values and Notes sections of that page.

  2. Alternatively, you could create more queries to grab each ID from the two tables (assuming you haven't altered the primary key in some way at one point or another). For example:

    SELECT task_assignID FROM task_assign ORDER BY task_assignID DESC
    SELECT notificationID FROM notification ORDER BY notificationID DESC
    
green.maru
  • 61
  • 1
  • 6