1

The problem is Weird actually sorry if i'm asking something stupid but i just want to know if it can be done or not....

Add.php
<form method="post" action="info.php">
<input name="Car_name" type="text">
<input name="Car_color" type="text">
<button type="submit" name="save" > save </button>
</form>

this page is add.php from here a post request is generated to page message.php. In this page we pass the Query like this..

Message.php
if(isset($_POST['save'])){
mysql_query("insert into Car_data where Car_name='".$_POST['Car_name']."',Car_color='".$_POST['Car_color']."'");
}

but i want to print the Auto incremented id of this inserted data.. For once i thought i can take a condition where i can check $_POST['Car_name']&&$_POST['Car_color'] but there can be many car with same color and name like red Ferrari.. Some is there any possible way to get id of that element just inserted ???

Markus Malkusch
  • 7,738
  • 2
  • 38
  • 67
Sarthak Sharma
  • 679
  • 2
  • 7
  • 17
  • possible duplicate of [How to get a Primary ID before or at the same time it gets created?](http://stackoverflow.com/questions/7192449/how-to-get-a-primary-id-before-or-at-the-same-time-it-gets-created) – Markus Malkusch Jan 11 '14 at 20:30

2 Answers2

2

Just call mysql_insert_id() after your insert (it returns the last inserted ID)

Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
2

To get the last inserted id you use mysql_insert_id()

mysql_query("insert into Car_data where Car_name='".$_POST['Car_name']."',Car_color='".$_POST['Car_color']."'");

$last_id = mysql_insert_id() ; 

But as its suggested use mysqli_* functions and can use mysqli_insert_id() function to get the last inserted id

Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63