Jump to content

[SOLVED] Very basic issue - generating random number or id number


nadz

Recommended Posts

hi, basically im making a simple registration script. ive got the script to allow users to input their name and password and email, it then logs that to my database into the fields email name password. but along with that i need to generate a random number to keep as the "id". "m doing this so that the users friends can access the users page by entering www.mysite.com/index.php?id=[id number]

 

any help would be appreciated. if there is anyway i can make the "id" the row number in the table that would be useful too. i just need to make it so that 2 users cant have the same id numbers

 

thankyou

Link to comment
Share on other sites

As long as you set your table up to auto increment a primary key every row will be given a unique ID number.

 

Say for example in a table "person" your primary key is "personid" you could get it like this:

<?php
  $fetch=mysql_fetch_assoc(mysql_query("SELECT `personid` FROM `person` WHERE `name`='joe'"));
  echo 'ID='.$fetch['personid'];
?>

Link to comment
Share on other sites

ie..

 

$query = "INSERT ...";

 

mysql_query($query);

 

$mynewid = mysql_insert_id();

 

important is that the table has an ID field (name doesn't matter but it should be INT, primary key and auto_increment)

 

even if it's a basic registration you should further think about protecting the 'account' (i think that's what you want after you asked the users for a password)

Link to comment
Share on other sites

hi again. turns out im having some problems displaying the id number in the page.

 

here is the code im using for the query and grabbing the id number

 

 

mysql_query("INSERT INTO `crush2` (name, email, password) VALUES ('$name', '$email', '$password')");
   
$id = $row["id"];
include("howto.php");

:

 

ive got this code in howto.php:

 

<a href="http://mysite.com/page.php?id=$id">copy this link and send it to your friends</a>

 

but it doesnt replace $id with the id from the database  ???

 

 

Link to comment
Share on other sites

ok, this is how the code looks now:

 

mysql_query("INSERT INTO `crush2` (name, email, password) VALUES ('$name', '$email', '$password')");
   
$id = mysql_insert_id();
include("howto.php");

 

but it still displays $id insted of the id number. maybe ive got the code wrong in the link. do i need square brackets around $id in howto.php?

Link to comment
Share on other sites

ive got this code in howto.php:

 

<a href="http://mysite.com/page.php?id=$id">copy this link and send it to your friends</a>

 

but i want it to output like this:

 

 

<a href="http://mysite.com/page.php?id=[the users id number]">copy this link and send it to your friends</a>

Link to comment
Share on other sites

change

 echo '<a href="http://mysite.com/page.php?id=$id">copy this link and send it to your friends</a>';

to

 echo '<a href="http://mysite.com/page.php?id='.$id.'">copy this link and send it to your friends</a>';

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.