Need Suggestions

This is a discussion on Need Suggestions within the MySQL Database forums, part of the Database Forums category; I need some suggestions on how to complete a tasks in an application I am working on. I have an ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-25-2007
John
 
Posts: n/a
Default Need Suggestions

I need some suggestions on how to complete a tasks in an application
I
am working on.

I have an order form that several users will be using on-line, which
dumps data into my MySQL Database.


I need to pull the next order number from the order table, which I
can
do successfully.


My problem is when more than one user is entering orders and they
save
the order there are then two records with the same order number in
the
system.


It doesn't duplicate the record since I have a unique field called
RecordID which is generated by the database.


So how can I insert the order number into the database when they open
the order form so if some else opens the form they get the next
number. I know I will proably run into problems with the numbering
if
they cancel the order and someone else as already created a new
one...but I don't think they will really care about that.


I have this code inserted into my order page:


-------------------CODE STARTS HERE-----------------------------
$query = "SELECT max(OrderNum) + 1 as OrderNum FROM
neworders";
if ($r = mysql_query($query))
{
while ($row = mysql_fetch_array($r))
{
$OrderNum = $row['OrderNum'];
echo $OrderNum;


}
}


$sql = "INSERT INTO neworders (OrderNum) VALUES ($OrderNum)
";
if (@mysql_query($sql))


mysql_close();
-------------------CODE ENDS HERE---------------------------


Which almost does what I want, except everytime the page is refresed
it pulls a new number, or when the push the next button on the form
it
pulls the next number and udpates all the information for that order
to that number, so then I have an empty order in the table.


Any help or suggestions would be greatly appreciated.


Thanks for all your help.


~John

Reply With Quote
  #2 (permalink)  
Old 06-25-2007
macca
 
Posts: n/a
Default Re: Need Suggestions

why are you trying to get the order number BEFORE you submit the
order?

If you are using an auto incrementing field this is not necesarry (as
it will auto-increment ;-) ) when you add a new record!
which you could then extract with the last insert id function.


But if you really have to do it this way, maybe you should consider
locking the table?

Reply With Quote
  #3 (permalink)  
Old 06-26-2007
Norman Peelman
 
Posts: n/a
Default Re: Need Suggestions

John wrote:
> I need some suggestions on how to complete a tasks in an application
> I
> am working on.
>
> I have an order form that several users will be using on-line, which
> dumps data into my MySQL Database.
>
>
> I need to pull the next order number from the order table, which I
> can
> do successfully.
>
>
> My problem is when more than one user is entering orders and they
> save
> the order there are then two records with the same order number in
> the
> system.
>
>
> It doesn't duplicate the record since I have a unique field called
> RecordID which is generated by the database.
>
>
> So how can I insert the order number into the database when they open
> the order form so if some else opens the form they get the next
> number. I know I will proably run into problems with the numbering
> if
> they cancel the order and someone else as already created a new
> one...but I don't think they will really care about that.
>
>
> I have this code inserted into my order page:
>
>
> -------------------CODE STARTS HERE-----------------------------
> $query = "SELECT max(OrderNum) + 1 as OrderNum FROM
> neworders";
> if ($r = mysql_query($query))
> {
> while ($row = mysql_fetch_array($r))
> {
> $OrderNum = $row['OrderNum'];
> echo $OrderNum;
>
>
> }
> }
>
>
> $sql = "INSERT INTO neworders (OrderNum) VALUES ($OrderNum)
> ";
> if (@mysql_query($sql))
>
>
> mysql_close();
> -------------------CODE ENDS HERE---------------------------
>
>
> Which almost does what I want, except everytime the page is refresed
> it pulls a new number, or when the push the next button on the form
> it
> pulls the next number and udpates all the information for that order
> to that number, so then I have an empty order in the table.
>
>
> Any help or suggestions would be greatly appreciated.
>
>
> Thanks for all your help.
>
>
> ~John
>


I would suggest that you use the 'RecordId' as the record number
using a command like sprintf or such to format it to your/the users
liking. Once the order is created, get the RecordId via the
last_insert_id command and store it in a session variable so that all
changes are done to the correct record. Something like:

1) user creates/retrieves record
2) get the last_insert_id (before the script ends) and store it in
$_SESSION['RecordId']
3) user makes changes/etc.
4) if $_SESSION['RecordId'] is set (not NULL or not 0 for example) then
perform an UPDATE
5) else perform an INSERT (for a new record)

....use the LAST_INSERT_ID() function from within an SQL statement, not
the mysq_last_insert_id() command as it may not return the proper data
under some circumstances.

$sql = "select LAST_INSERT_ID()";

Norm
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:07 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0