|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
SELECT statement
I am using PHP to query a MySQL database. I want to insert a new record into a table, then retrieve the new record.
My first query inserts the data (since Org_ID is auto_increment/not null, a new ID is created). How should I structure the second query? The way I have it shown does not work since it is an improper use of the Max() function. What else would work? $query1 = 'INSERT INTO tblorganizations SET Name = "$Name", Address = "$Address", City = "$City", State = "$State", PostalCode = "$PostalCode", Referral = "$Referral", Org_Type_ID = "$Org_Type_ID", Affiliates = "$Affiliates", Remarks = "$Remarks" '; $query2 = 'SELECT * FROM tblorganizations WHERE Org_ID = MAX(Org_ID) '; |
|
#2
|
|||
|
|||
|
First of all, your INSERT statement is not correct, :
$query1 = 'INSERT INTO tblorganizations SET Name = "$Name", Address = "$Address", City = "$City", State = "$State", PostalCode = "$PostalCode", Referral = "$Referral", Org_Type_ID = "$Org_Type_ID", Affiliates = "$Affiliates", Remarks = "$Remarks" '; Should be: INSERT INTO tblorganizations VALUES (NULL, '$Name', '$Address', '$City', '$State', '$PostalCode', '$Referral', '$Org_Type_ID', '$Affiliates', '$Remarks'); for the second query, PHP has a function called mysql_insert_id(), that retrieves the ID of the latest inserted row (see the PHP manual for this function), however, this works only if you use directly after your insert statement. A workaround: $query2 = "SELECT MAX(Org_ID) FROM tblorganizations"; $temp = mysql_fetch_row($query2); $query3 = "SELECT * FROM tblorganizations WHERE Org_ID = ".$temp[0]; MySQL does not support subqueries yet, so that's why I used the query2... This was done quick, so it probably isn't completely correct, but it should help you get underway.... Last edited by partyganger : April 12th, 2002 at 02:50 AM. |
|
#3
|
|||
|
|||
|
Thanks!
mysql_insert_id() is exactly what I was looking for. I tested it and it works fine. By the way, my syntax for query1 was correct. Check it out! Thanks again. |
|
#4
|
|||
|
|||
|
yeah he's right. it's a weird insert syntax but it works...its like the "underground" version of insert into...values hehe
so you got everyting sorted out and your queries are fine now? |
|
#5
|
|||
|
|||
|
Yes.
You can close the thread now if you like. Thanks! |
|
#6
|
|||
|
|||
|
noo dont close threads!
people can use the search function if they have a similar problem - and then they can probably figure out their problems from this thread! ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > SELECT statement |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|