473,399 Members | 4,192 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,399 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 3113
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.