
paulW
Posts: 24
|
| Posted: 09/09/2009, 11:28 AM |
|
I must be having a brain fart, but for some reason I am blanking out on this....
I have a POS application that takes a foriegn key (user_id) from an admin, and needs to start a new order for the user whose id was entered. When the admin enters a user_id, it is inserted into the dbo_orders table, where the primary key, order_id, is autoincremented. I would like, post-INSERT, to retrieve that order_id (something like SELECT TOP order_id FROM dbo_orders WHERE user_id= $form->user_id) , and add ?order_id=returned value to the end of the URL. Anyone know the best way?
|
 |
 |
lvalverdeb
Posts: 299
|
| Posted: 09/09/2009, 11:41 AM |
|
If you use mysql, the last inserted id is returned by mysql_insert_id(). I use this solution in the afterexecuteinsert event.
$last_id = mysql_insert_id();
if ($last_id) {
$order_id = $last_id;
.....
.....
}
_________________
lvalverdeb
CR, GMT-6
XAMPP/Ubuntu/CCS3.2/4 |
 |
 |
jjrjr1
Posts: 942
|
| Posted: 09/09/2009, 12:09 PM |
|
Using Ivalverdeb's code
In the after execute insert event with action being custom code
Try
global $Redirect;
$last_id = mysql_insert_id();
if ($last_id) {
$order_id = $last_id;
$Redirect = "The_Page_You_Want.php?".$order_id;
.....
.....
}
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
paulW
Posts: 24
|
| Posted: 09/09/2009, 12:30 PM |
|
Thanks guys, I remembered the global $Redirect trick shortly after I posted ( I hate brain farts! )
I am not using MySQL (unfortunately) so here's what I did to make it work with SQL Server:
I took the 'Return Page' attribute off my form, and un-coupled the fields from their database columns. Then, instead of writing custom code for the After Insert... event, I added a Server-side "on click" event to my submit button. In the custom code, i wrote my insert statement: INSERT INTO dbo_orders (userID) VALUES ($userID) then got the order_id this way: SELECT TOP 1 orderID FROM dbo_orders WHERE userID=$userID ORDER BY order_id DESC , the idea being that since an admin would be putting this order in while the customer is in front of them, there is no way the user can by putting an order in at the same time, which could cause orders to get messed up...
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 09/09/2009, 7:20 PM |
|
In SQL Server would you not use
SELECT IDENT_CURRENT(’tablename’)
To get the last inserted record ID????
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
paulW
Posts: 24
|
| Posted: 09/10/2009, 5:02 AM |
|
Yes, but I needed to be able to specify the userID when requesting the orderID, for the edge cases where another user starts an order at the same time.
|
 |
 |
jjrjr1
Posts: 942
|
| Posted: 09/10/2009, 6:17 AM |
|
That should not be the case.
That call will retreive the last ID created by that session.
In other words if you get the IDENTITY value in the after execute insert event, the value returned will be the record ID for the previous transaction in that session.
_________________
John Real - More CodeCharge Studio Support at - http://CCSElite.com |
 |
 |
|

|
|
|
|