473,231 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 software developers and data experts.

recall id after adding a query to MySQL database

Hello,

Lately I came accross this problem a couple of times, and I know there
must be a clever solution. However, I did not find it untill now.

When I add a entry to my mysql database from a PHP form, it gives the
document an 'id number', which is generated by MySQL. In my PHP script
I would like to continue with id, since that is easier. At the moment
I place a SELECT query in which I basically match the values I still
have in my PHP_POST with the values in the database (those I just
entered).

I have the feeling that there is a simple code for replacing those
lines of PHP script and unnecessary queries. Is there somebody who can
help me, please?

Jochem
Jul 17 '05 #1
2 3103
In article <2e**************************@posting.google.com >,
jd******@operamail.com (Jochem) wrote:
Hello,

Lately I came accross this problem a couple of times, and I know there
must be a clever solution. However, I did not find it untill now.

When I add a entry to my mysql database from a PHP form, it gives the
document an 'id number', which is generated by MySQL. In my PHP script
I would like to continue with id, since that is easier. At the moment
I place a SELECT query in which I basically match the values I still
have in my PHP_POST with the values in the database (those I just
entered).

I have the feeling that there is a simple code for replacing those
lines of PHP script and unnecessary queries. Is there somebody who can
help me, please?


Assuming the document ID number is the result of a MySQL auto_increment
field,

$result = mysql_query('INSERT INTO foo SET bar=1', $db);
$id = mysql_insert_id();
<http://php.net/mysql_insert_id>

or

$result = mysql_query('INSERT INTO foo SET bar=1', $db);
$result = mysql_query('SELECT LAST_INSERT_ID() AS id', $db);
$myrow = mysql_fetch_array($result);
$id = $myrow[id];

The mysql_insert_id() function will return the numeric value of the
auto_increment field of the last inserted record. The MySQL query for
LAST_INSERT_ID() will return the same.

Happy Holidays!

hth

--
Bulworth : fu***@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!
Jul 17 '05 #2
Thanks that was my missing link!
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DBNovice | last post by:
I have a database that keeps records on the issue and failure of an item. Currently, the database is poorly desisned; therefore I'm performing queries to break the data into normalized tables and...
3
by: Paradigm | last post by:
I am using Access 2K as a front end to a MYSQL database. I am trying to run a Union query on the MYSQL database. The query is (much simplified) SELECT as ID from faxdata UNION SELECT as ID ...
6
by: sheree | last post by:
I would like to create a query where one of the columns of the queries comes from a combo list box on a form. For example, if my table has the following fields: id name interest1 interest2...
3
by: news | last post by:
Hi all, hope you can help me. I have recently set up a mysql database of local pubs, with a web form to add new listings (using PHP to generate an INSERT query). I've just been doing a bit of...
9
by: noor.rahman | last post by:
I was wondering how it may be possible to query 2 MySQL databases using one query statement from PHP. For instance: SELECT database1.tableA.field1 UNION database2.tableB.field2. My concern...
7
by: Daz | last post by:
Hi. I am trying to select data from two separate MySQL tables, where I cannot use join, but when I put the two select queries into a single query, I get an error telling me to check my syntax. Both...
2
by: basestring | last post by:
Hi I am busy now for many hours without luck I have a database and when I use PHP to add date to it, it works only one time when i want to add the next data, It doesn't work. but i don't get any...
5
by: deaconj999 | last post by:
Hi, I have nearly finished my database and I would like to add a query that uses a combo box to get the results, not the usual paramater style input. I suppose it would need a form and a query...
1
by: tomlebold | last post by:
Having problems displaying query results from combo boxes on a sub form, which is on the same form that is used to select criteria. This has always worked form me when displaying query results on...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.