nadz Posted July 6, 2007 Share Posted July 6, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/ Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 If users are registering and you're INSERTing their details into a table then you can use the ID number of the row. Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291317 Share on other sites More sharing options...
nadz Posted July 6, 2007 Author Share Posted July 6, 2007 thanks for the reply. just wondering how would i use that. im guessing $id or $row? Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291320 Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291323 Share on other sites More sharing options...
melvincr Posted July 6, 2007 Share Posted July 6, 2007 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) Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291325 Share on other sites More sharing options...
nadz Posted July 6, 2007 Author Share Posted July 6, 2007 thanks for the help guys. its much appreciated. the db has a field now that has the id number in it. Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291337 Share on other sites More sharing options...
nadz Posted July 6, 2007 Author Share Posted July 6, 2007 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 ??? Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291360 Share on other sites More sharing options...
Yesideez Posted July 6, 2007 Share Posted July 6, 2007 You need to add the mysql_insert_id() command immediately after your mysql_query("INSERT... to get the last insert ID. Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291362 Share on other sites More sharing options...
nadz Posted July 6, 2007 Author Share Posted July 6, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291370 Share on other sites More sharing options...
per1os Posted July 6, 2007 Share Posted July 6, 2007 Where are you trying to print out $id. If it is encapsulated within single quotes variable names are taken literally. Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291373 Share on other sites More sharing options...
nadz Posted July 6, 2007 Author Share Posted July 6, 2007 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> Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291378 Share on other sites More sharing options...
sasa Posted July 6, 2007 Share Posted July 6, 2007 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291381 Share on other sites More sharing options...
nadz Posted July 6, 2007 Author Share Posted July 6, 2007 hi, thankyou all for your help. ive got it working - it prints the id now. thanks again Quote Link to comment https://forums.phpfreaks.com/topic/58730-solved-very-basic-issue-generating-random-number-or-id-number/#findComment-291397 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.