So when I plan to set up a register form on my site I need to know how to automatically assign an ID to the user once they press the 'Register' button.

I guess I would be using this type of table setup;

Table name: Users

ID int 11
Email varchar 30
Password varchar 15
Firstname varchar 30
Lastname varchar30


if the Id Column is set to auto increment, You can use this sql statement

$sql="Insert into users values("NULL","", "firstname","lastname")"

my small 5c:
create table Users(
ID int(11) auto_increment primary key,
Email varchar (30) not null,
Password varchar(15) not null,
Firstname varchar(30) not null,
Lastname varchar(30) not null,

after you execute query you can retrieve the ID using mysql_insert_id()

If you're going to do DB operation in more professional way use "prepared statements" which are nicelly realized in mysqli extension

But if you want to go even more professional -- use ORM

Member Avatar


If I were you I'd add an email confirmation script. Once the user registers, the users table is updated with the id, but an additional field called confirmed is set to 0 (meaning NO). It is only set to 1 (YES) when the user replies to the email by pressing a link contained within. Otherwise, you may be bombarded with spammers and other flavours of magic carpet idiot.

This is quite easy to do - just build a confirm key - which can be made from a hash of email and id, e.g.

$salt = "f00l15h"; //or any string of your choice
$confirm = md5($salt . $email . $id);

$url = "$id&confirmcode=$confirm";

Just send the $url in the body of the welcome email.

The confirm.php page then processes the hash - if it matches - set confirm field to 1



Get the email ($email) from the DB using id as a WHERE clause value.
Check that the confirm code matches:

$salt = "f00l15h";
if($_GET['confirmcode'] == md5($salt . $email . $id))....

Anyway, that's my 2penneth.

Don't forget to make the ID field in MySQL unsigned and NOT NULL.
ID int(11) unsigned NOT NULL auto_increment primary key