Page 1 of 1

Check last value of a field and then increment it by one

Posted: Sun Feb 14, 2010 4:27 am
by rbhoumik
Hi,
I desperately need some help here. Can anybody tell me how is it possible to check the last value of a field and then increment that value by one and then insert the incremented-value into that field?

Example

Table:Books

Book id - Name - Purchase Year - Accession Number
1 - x - 2000 - 1.2000
2 - y - 2010 - 1.2010
3 - z - 2004 - 1.2004


Now, with the help of a form, the user input the book name and purchase year ( for eg. a,2010). Then a checking needs to be done to get the last value of the accession number field where accession number = ((digit).2010), then add one to that value and gets inserted into the field. So the table becomes.

Book id - Name - Purchase Year - Accession Number
1 - x - 2000 - 1.2000
2 - y - 2010 - 1.2010
3 - z - 2004 - 1.2004
4 - a - 2010 - 2.2010



Please tell me if there is any confusion as I need this solution urgently...Thank you

Re: Check last value of a field and then increment it by one

Posted: Sun Feb 14, 2010 5:47 am
by kalidG
I think you can SELECT every thing from the table where the year is equal to desired year then check the number of rows (You can use the function mysql_num_rows() )returned for example if there are 4 records where the year equal to 2010 for example add 1 to this number and append it.

Re: Check last value of a field and then increment it by one

Posted: Sun Feb 14, 2010 6:55 am
by requinix
And what happens if two people try to do this at the same time? Person A gets 2.2010, person B gets 2.2010, person A inserts value, person B inserts value.

Re: Check last value of a field and then increment it by one

Posted: Sun Feb 14, 2010 9:55 pm
by rbhoumik
tasairis wrote:And what happens if two people try to do this at the same time? Person A gets 2.2010, person B gets 2.2010, person A inserts value, person B inserts value.
No, there will be only one user for inserting details...and that's for certain

Re: Check last value of a field and then increment it by one

Posted: Sun Feb 14, 2010 9:56 pm
by rbhoumik
kalidG wrote:I think you can SELECT every thing from the table where the year is equal to desired year then check the number of rows (You can use the function mysql_num_rows() )returned for example if there are 4 records where the year equal to 2010 for example add 1 to this number and append it.
Thank you...I will definately try this...

Re: Check last value of a field and then increment it by one

Posted: Sun Feb 14, 2010 10:09 pm
by John Cartwright
rbhoumik wrote:
tasairis wrote:And what happens if two people try to do this at the same time? Person A gets 2.2010, person B gets 2.2010, person A inserts value, person B inserts value.
No, there will be only one user for inserting details...and that's for certain
I believe he is talking about a possible race condition.