Jump to content

.mysql_insert_id();


dropfaith

Recommended Posts

Okay So what im trying to do inserts a record into database then i need to pull the id field back out and display it in the form of a link like below i know how to pull the id field of the inserted row but it wont display in the link like below any ideas?

 

its for a business index where they enter the fileds for the business  then it proceeds to a form to add the hours of the business

 

<a href=addhours.php?Id=8>

Link to comment
https://forums.phpfreaks.com/topic/121031-mysql_insert_id/
Share on other sites

oh this is simple :D

 

<?php
$id = $_POST['id'];
$query = "INSERT INTO `table_name` (id) VALUES ('$id')";
mysql_query($query);

$r = mysql_query("SELECT * FROM `table_name`");

$row = mysql_fetch_array($r);
id = $row['id'];
echo '<a href=addhours.php?Id='.$id.'>ID: '.$id.'</a>'; // 1 results

while ($row = mysql_fetch_array($r)){
id = $row['id'];
echo '<a href=addhours.php?Id='.$id.'>ID: '.$id.'</a><br>'; // ALL results
}


?>

Link to comment
https://forums.phpfreaks.com/topic/121031-mysql_insert_id/#findComment-623917
Share on other sites

Adding to ProjectFear's post, in the manual it is stated:

 

Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

 

Meaning that you must call it before making any other queries after the insert one.

Link to comment
https://forums.phpfreaks.com/topic/121031-mysql_insert_id/#findComment-624004
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.