Re: POST a variable

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Do a 'select @@identity', it will give you the last record id. The gap in between steps 3 and 4, brief as it may seem, is enough to get you in trouble.

Satyam



----- Original Message ----- From: "Dan Shirah" <mrsquash2@xxxxxxxxx>
To: "Németh Zoltán" <znemeth@xxxxxxxxxxxxxx>
Cc: "php-general" <php-general@xxxxxxxxxxxxx>
Sent: Friday, March 23, 2007 4:07 PM
Subject: Re:  POST a variable


The reason I have to use it as I posted is because I am using Microsoft SQL
server instead of MySQL.  And I haven't found a php function for MSSQL that
works the same as mysql_insert_id()

So, to come out with a comparable function with pretty reliable results, I
follow this process:

1. User enters data into form
2. User submits form
3. Save page inserts info into the database
4. Directly after the insert statement is my SELECT MAX query
5. I assign the retrieved value to a hidden field
6. I pass this value to the next form

I figure the odds of another record being inserted inbetween the time it
takes to go from step 3 to step 4 are very, very minimal.  We're talking
about MAYBE a 2-3 millisecond gap?


On 3/23/07, Németh Zoltán <znemeth@xxxxxxxxxxxxxx> wrote:

2007. 03. 23, péntek keltezéssel 10.45-kor Dan Shirah ezt írta:
> Okay, I feel like the correct answer to this is about 2mm back in my
grey
> matter.
>
> 1. I have a query that pulls the last row number when a newly inserted
> record is added:
>
> $maximum="SELECT MAX(payment_id) FROM payment_request";
>   $max_result=mssql_query($maximum);
>   while($max=mssql_fetch_row($max_result)){
>   }
>   $max_id = $max[0];
>
> 2. I have multiple selections for the user to pick, but regardless of
what
> they choose I want the $max_id variable to be passed to the next page.
>
> 3.  Would I go about this by assigning $max_id to a hidden field like
below?
>
> <input type="hidden" value="<?php echo "$max_id"; ?>" size="5"
> maxlength="10" name="max_id" />
>
> 4.  And then to retrieve this value on my next page just get it out of
> $_POST['max_id']  ??
>
> Does that all sound correct?

basically yes
but if you want the id of the row you just inserted, using
mysql_insert_id() is better because if another insert is happening at
the same time, select max() may give you incorrect result

greets
Zoltán Németh





--------------------------------------------------------------------------------


No virus found in this incoming message.
Checked by AVG Free Edition.
Version: 7.5.446 / Virus Database: 268.18.17/730 - Release Date: 22/03/2007 7:44

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux