Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getting the last inserted record 1

Status
Not open for further replies.

taval

Programmer
Jul 19, 2000
192
0
0
GB
Hi,

I was wonder how would I get the last inserted record, as I want to display the record I just inserted on the screen.

I was thinking I might need another
$sqlResult = @mysql_query("Select * FROM Customer WHERE Name='$Name'");
after the insert query.

But how would I select the record using the CustomerID rather then the Name. The thing is I can't get the CustomerID since it is auto incremented.

My code looks something like

---

$sqlResult = @mysql_query("INSERT INTO Customer VALUES (NOT NULL, '$Title', '$Name', '$Address1', '$Address2','$CSP','$Postcode','$City','$Country', '$Telephone', '$Email','$CardType','$CardNumber','$IssueNumber', '$Year''$Month''01','$CardHolder')");


while ( $row = mysql_fetch_array($sqlResult) )
{
echo(&quot;<TABLE>&quot;);
echo(&quot;<TR><TD>CustomerID : </TD><TD>&quot; . $row[&quot;CustomerID&quot;] . &quot;</TD></TR>&quot;);
echo(&quot;<TR><TD>Title : </TD><TD>&quot; . $row[&quot;Title&quot;] . &quot;</TD></TR>&quot;);
echo(&quot;<TR><TD>Name : </TD><TD>&quot; . $row[&quot;Name&quot;] . &quot;</TD></TR>&quot;);
....
....
....

-----

Grateful for any help, thanks.
 
You could add a timestamp column and do a @mysql_query(&quot;Select * FROM Customer WHERE Name='$Name' ORDER BY timestampcolumn&quot;) and just grab the top entry... -Nukoi
I know my stuff.. Hire me.
 
Oh, you might also have to add DESC after timestampcolum in the query.. I cant remember if timestamps show up by oldest or newest first by default. -Nukoi
I know my stuff.. Hire me.
 
Hi,

Its giving me a parse error with the query??

@mysql_query(&quot;Select * FROM Customer WHERE Name='$Name' ORDER BY timestampcolumn&quot;)

@mysql_query(&quot;Select * FROM Customer WHERE Name='$Name' ORDER BY timestampcolumn DESC&quot;)
and

Would there be any other way of just extracting the CustomerID of the last record inserted?

Thanks.
 
Did you add a timestamp column to the table before you tried the queries involving timestamp? If you just modified the query without adding the appropriate column to your table, that may explain why you're getting errors with the query.

I have done something like this by performing another query after the insert statement, as you suggest in the original post. I have no idea if this is a very efficient way to do it, but I was unsure of how basing a query on a timestamp (or any other way of determining the ID for the row that was just most recently inserted) might get complicated if ever a couple of users should be doing things at the same time.

Seems like there would be a function to return the ID of a row just inserted, though I've not yet stumbled across it. Matt
matt@paperlove.org
If I can help, I will.
 
Dum Tech..

I thought about multiple users submitting things at the same time also, thats why I added WHERE Name='$name' to the query.. It will return the last thing added by that person. -Nukoi
I know my stuff.. Hire me.
 
mysql_data_seek allows you to skip through the record set to the specified row number, starting with zero.

Don't know how many results there are in aparticular query? mysql_num_rows will tell you.

Therefore, mysql_data_seek($result, mysql_num_rows($result) - 1) should always be the last record. Be sure to include the -1, as the number of rows starts counting from 1, not zero.
 
did u try to use the following statement after inserting the new customer.

$result = mysql_query(&quot;select last_insert_id() as lastid&quot;);
$lastid = mysql_result($result,0)

this will return the last id inserted by the connection that you are using.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top