Giving a customer an ID number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
x2designs
Forum Newbie
Posts: 2
Joined: Wed Dec 10, 2003 9:21 am
Location: Hampshire

Giving a customer an ID number

Post by x2designs »

OK, I have a client list stored in a MySQL database and each time a new row is entered it auto_increases the ID column. So now the client has an ID number

However, I want to be able to do this before entering their name into the database, making sure that the number they are given is only ever used once.

Any help appreciated

Ian
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

if you have MySQL, the auto_increment field IS unique.. nothing you can do about it... it's MUCH safer to let mysql handle this then to do it in PHP...
x2designs
Forum Newbie
Posts: 2
Joined: Wed Dec 10, 2003 9:21 am
Location: Hampshire

Post by x2designs »

I suppose I could always generate the ID number in the database, then echo it on the webpage, then when it comes to finally inserting all the data. Just use Update where id = $id.
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

why do you need the customer ID up front?
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

As long as you have auto_increment on, when you do the first MySQL query (such as right after the customer has registered) just leave the ID field blank and MySQL will take care of the rest.

so:

Code: Select all

<?php
$query = "INSERT INTO table VALUES ('','$customername','$customerinfo')"; // the first '' is where the id column is
?>
Then whenever you retrieve info from the database first, just make sure you keep the $id and then later use it to update the database.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

DuFF wrote:As long as you have auto_increment on, when you do the first MySQL query (such as right after the customer has registered) just leave the ID field blank and MySQL will take care of the rest.

so:

Code: Select all

<?php
$query = "INSERT INTO table VALUES ('','$customername','$customerinfo')"; // the first '' is where the id column is
?>
Then whenever you retrieve info from the database first, just make sure you keep the $id and then later use it to update the database.
Just adding the following based on DuFF's post:
PHP Manual wrote: mysql_insert_id() returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query using the given link_identifier. If link_identifier isn't specified, the last opened link is assumed.
Look that one up for more info. Might be interesting.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Any NULL value will do.

NULL, 0, '', and the likes.
User avatar
aquila125
Forum Commoner
Posts: 96
Joined: Tue Dec 09, 2003 10:39 am
Location: Belgium

Post by aquila125 »

or just skip give the column names.. is easier to read and less error prone:


$query="INSERT INTO table (column1,column2) values (value1,value2)";
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

aquila125 wrote:if you have MySQL, the auto_increment field IS unique.. nothing you can do about it... it's MUCH safer to let mysql handle this then to do it in PHP...
it is unique... but once one is deleted.. then the numbers go all screwed... like if u had 10, 11, 12, 13 and u deleted 11, then it would be weird... :roll:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

why would it be weird, all numbers would be still unique.

Does it matter that customer id numbers are in sequence!?

Mark
Last edited by JayBird on Thu Dec 11, 2003 5:51 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

AnsonM wrote:
aquila125 wrote:if you have MySQL, the auto_increment field IS unique.. nothing you can do about it... it's MUCH safer to let mysql handle this then to do it in PHP...
it is unique... but once one is deleted.. then the numbers go all screwed... like if u had 10, 11, 12, 13 and u deleted 11, then it would be weird... :roll:
That shouldn't make any difference to you it's just an identifier for that row - reusing unique id's is a bad idea and if your display depends on unique id's being sequential then you need to change your code so that it doesn't.

Mac
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

Bech100 wrote:why would it be weird, all numbers would be still unique.

Does it matter that customer id numbers are in sequence!?

Mark
lol yeah... sometimes :? 8O :oops:
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

When, for example, id number 11 is deleted nothing "wierd" happens. MySQL just continues incrementing the last id that was set. So if you had 30 customer ID's and deleted number 11, then MySQL would just continue to 31, 32, 33 . . .
Post Reply