This question already has an answer here:
I'm running a MySQL INSERT query where I insert some data into the database. Each row has a unique ID generated each time a new entry is added. I'd like to get this ID straight after I insert the data.
PHP Code:
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
//Here I need to get the row id of the above data...
Thanks!
</div>
Try like
$addGiveawayToDb = mysql_query("INSERT INTO $table(orientation, title, color)
VALUES ('$orientation', '$title', '$color')")
or die(mysql_error());
echo "Inserted Id is ".mysql_insert_id();
Makesure that you have an auto increment field in your table.And also try to avoid mysql_*
functions due to they are deprecated.Instead use mysqli_*
functions or PDO
statements.
See this LINK