Computer Webmaster Gaming Console Graphics Forum

Welcome to the Computer Webmaster Gaming Console Graphics Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

MK PitStop Main Earn $25 Earn Money Posting Extras Members Blogs Image Hosting User Pages
Go Back   Computer Webmaster Gaming Console Graphics Forum > Webmaster Forum > Website Coding > Database
Register FAQ/Rules Become A V.I.P. Member Search Today's Posts Mark Forums Read

Database Database problems or need to ask a question? maybe something to do with sql injections or a database software question. Database topics cover MySQL, PostgreSQL, Oracle, SQL Server or anything else related to databases.

Google
Closed Thread
 
LinkBack Thread Tools Display Modes
Old 07-02-2007, 10:59 AM   #1
John
 
John's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

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

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Advertisements
Old 07-02-2007, 10:59 AM   #2
macca
 
macca's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default 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?

 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Old 07-02-2007, 10:59 AM   #3
Norman Peelman
 
Norman Peelman's Avatar
 
Posts: n/a
My Photos: (0)

Banked:
MK Cash: $

I am Worth:
MK Cash: $
Donate

Recent Blog: None

Default 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
 
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit!
Featured Websites
Free Space
Free Space
Free Space Free Space
Closed Thread
Tags:




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Suggestions? Paul Liversidge PHP 0 07-01-2007 3:31 PM
Any suggestions? Mark Parnell HTML 0 07-01-2007 1:20 PM
suggestions? PoetStorm HTML 0 07-01-2007 1:20 PM
Bike GPS - suggestions? ric GPS 13 06-26-2007 12:08 AM
Thread suggestions Scoot Bugs And Feedback 14 05-22-2006 6:22 PM


Featured Websites




All times are GMT +1. The time now is 12:23 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.0.0
Cheap Computers
MK PitStop Copyright 2005 - 2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98