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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.