Jump to content

Autogenerate ID code continuing from last DB entry but not writting to it


chet139

Recommended Posts

I have an order page for which I want the user to be able to generate an orderid for a particular order before starting to add items to the order.

 

Now this is not a customer system and is intended to be used only by internal staff.

 

Basically all I am after is a button, which once the user presses it should supply an ordernumber BUT which is not already in the database. So if 1,2,3,4 are in already in 5 should be returned, but not added to the DB. I only want to use it as a variable for now.

 

If this does not make sense please ask..... I am new to this LOL....

Link to comment
Share on other sites

Thanks for the responses.

 

Firstly for the purpose of this project there is no issues of multiusers, but certainly a valid point. Thank you.

 

Bauer418 - your idea is exactly what I need. But being new to this I am at a lost of how to put that into code - Though it is likely quite simple.  I would run the query which in mycase would be:

 

SELECT MAX (orderId) + 1 from order

 

But from that all I would want is the orderId+1 result stored in some var as I would not be adding to the DB until later on......can you help?

 

Thanks!

Link to comment
Share on other sites

It does not work, I have had to make a few naming changes and i realised its ordId not orderId - but thats not the problem.

$query = "SELECT MAX (ordId) + 1 AS newOrderId FROM order";// query to generate new order ID
	$res = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_assoc($res);
	$new_orderid = $row['newOrderId'];

 

then to use the new var i have used it as so..

 

Order ID:</b><br/> <input type ="text" value ="<?php $new_orderid?>" name="orderId">

 

but upon load this page (its all on one page the following error is return)

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order' at line 1

 

NOTE: on line 1 there is nothing apart from "<html>".

 

I tried running the query from mysql query browser and it does not work there either....

 

 

Link to comment
Share on other sites

Nope still no joy

 

tried

$query = "SELECT MAX (ordId) + 1 AS newOrderId FROM 'order'";// query to generate new order ID
	$res = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_assoc($res);
	$new_orderid = $row['newOrderId'];

 

Gives same error.

 

Also tried but with no joy -

 

 

$query = "SELECT MAX (ordId) + 1 AS newOrderId FROM furniture.order";// query to generate new order ID
	$res = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_assoc($res);
	$new_orderid = $row['newOrderId'];

 

But gives following error:

 

FUNCTION furniture.MAX does not exist

Link to comment
Share on other sites

Two possible problems:

 

1--Your MySQL doesn't like the space you're including between MAX and (ordid)...so try MAX(ordid)...yes, it's ridiculous, but I've seen more ridiculous things.

 

2--Your version of MySQL does not support the MAX function (which is what the error seems to be saying).  At that point, you could always do something like:

 

SELECT id FROM order ORDER BY id DESC LIMIT 1[code=php:0]

 

...which will give you the highest order number on the table.  Then...just add 1 before display.

Link to comment
Share on other sites

Further to this,

 

I cleared by whole DB table and it transpired that  once the DB is empty the orderId is not autoset to 1.

 

Any help appreciated the code is as below.

 

Like I said works fine accept it wont generate new ID if DB is empty.

 

                $query = "SELECT MAX(custId) + 1 AS newcustId FROM `customer`";// query to generate new order ID
	$res = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_assoc($res);
	$new_custid = $row['newcustId'];

 

 

Link to comment
Share on other sites

Ok, so I skimmed over this and sorry if it is totally off topic but here it goes:

 

To me the database designed is flawed. Usually with a cart system you have two tables for orders, "orders" which contains the order information IE who, when, where email etc. The second table, which uses the orderid from the "orders" table is called "orderitems" which contains an orderitemid, orderid, itemid.  From there you can create order ids and then dynamically add items to the order by simply using that id, and removing items in a similar fashion. I am sure there was a tutorial on who to set it up in depth, but what you are essentially doing with the logic I put above is making the database in 3rd Normal Form, which is the standard for a database to be in and the preferred/efficient method.

 

Again sorry if I mis-read this completely.

Link to comment
Share on other sites

Thanks for your input again Bauser  I will give that a try later today

 

The database design is not flawed and is normalised.

 

An order can have many products and a product can be on many orders. Therefore you have a many to many relationship. IN terms of long term storage, with regards to the order, there is an order table which holds no product details of an order ! as that would not be a good idea!

 

So an orderline table will be created.

 

Thanks

Link to comment
Share on other sites

Ok,

 

So I used

 

if (!$new_orderid || !is_integer($new_orderid))
	{
		$new_orderid = 1;
	}

 

(Excuse the change of Var names)

 

Instead of 0 i wanted 1 so i changed it. It works however once a record has been added with ID of 1, the code still returns 1 as next ID.

 

The code I have now is below:

 

                $query = "SELECT MAX(ordId) + 1 AS newOrderId FROM `order`";// query to generate new order ID
	$res = mysql_query($query) or die(mysql_error());
	$row = mysql_fetch_assoc($res);
	$new_orderid = $row['newOrderId'];
	if (!$new_orderid || !is_integer($new_orderid))
	{
		$new_orderid = 1;
	}

Link to comment
Share on other sites

$query = "SELECT MAX(ordId) + 1 AS newOrderId FROM `order`";// query to generate new order ID
$res = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($res);
$new_orderid = $row['newOrderId'] ? $row['newOrderId'] : 1;

That should do it...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.