keloa Posted November 11, 2013 Share Posted November 11, 2013 Hi I'm working in a small project and I need to make a random number that doesn't repeat.The activation codes are stored in the database (mysql database) but the thing is that I want php to do the following: 1- create a random number (I used the function (rand) and it did exactly what I needed ). 2-check if the code is already stored in the database and if it's already there it should create another code (which is not stored in the database). Thanks Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/ Share on other sites More sharing options...
dalecosp Posted November 11, 2013 Share Posted November 11, 2013 Very nice. So, um ... you have code that does this? Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1457883 Share on other sites More sharing options...
keloa Posted November 11, 2013 Author Share Posted November 11, 2013 (edited) Thanks but actually I'm trying to find the code Edited November 11, 2013 by keloa Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1457899 Share on other sites More sharing options...
mac_gyver Posted November 11, 2013 Share Posted November 11, 2013 programming help forums are not here to find or to write code for you. they are for helping with code you have written. for your 2nd listed task, where exactly are you stuck at in doing this? without your attempted code that queries your database and what result or problem you had when you ran it, we don't have a clue what to help you with. Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1457901 Share on other sites More sharing options...
keloa Posted November 11, 2013 Author Share Posted November 11, 2013 New_order.php <?php session_start(); include ("inc/config.php"); include ("inc/header.php"); if(!isset($_SESSION['user'])){ header("Location: login.php"); }else{ ?> <center> <form method="POST" action="check_order.php"> عنوان الطلب : <input type="text" name="title" /><br /> تفاصيل الطلب :<textarea name="describtion"></textarea><br /> رابط الطلب ان وجد : <input type="text" size="70" name="url" /><br /> <input type="submit" value="تقديم الطلب" /> </form> </center> <?php include("inc/footer.php"); } ?> ^ it works fine the second file is : check_order.php <?php session_start(); include ("inc/config.php"); include ("inc/header.php"); if(!isset($_SESSION['user'])){ header("Location: login.php"); }else{ $title = $_POST['title']; $describtion = $_POST['describtion']; $url = $_POST['url']; $username = $_SESSION['user']; $track_number = rand(10000, 99999); if(empty($title)or empty($describtion)){ echo "الرجاء ملأ جميع الحقول المطلوبة"; }else{ $sql="SELECT * FROM serv WHERE(track_number='".$track_number."')"; $result = mysql_query($sql); $asc = mysql_fetch_assoc($result); $asc['tracknumber']; while ($track_number == $asc['tracknumber']) { $track_number = rand(10000, 99999); } $sql = "INSERT INTO serv(username,title,describtion,track_number,url) VALUES('$username','$title','$describtion','$track_number','$url')"; $result = mysql_query($sql); if(isset($result)){ echo "done"."<br />"."Your tracking number is $track_number"; }else{ echo "check your code"; } }} ?> The result: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files (x86)\EasyPHP-DevServer-13.1VC9\data\localweb\firstproject\check_order.php on line 20doneYour tracking number is 10175 The problems are : 1-The error on line 20 2-the data didn't enter the database Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1457904 Share on other sites More sharing options...
Ch0cu3r Posted November 11, 2013 Share Posted November 11, 2013 The error on line 20 is caused by the query on line 19 failing probably due to the parenthesis $sql="SELECT * FROM serv WHERE(track_number='".$track_number."')"; Remove the ( and ) from the query. Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1457909 Share on other sites More sharing options...
ignace Posted November 11, 2013 Share Posted November 11, 2013 Why don't you simply use the order #ID? Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1457941 Share on other sites More sharing options...
Solution dalecosp Posted November 11, 2013 Solution Share Posted November 11, 2013 (edited) Have you considered simply using uniqid()? $track_number = uniqid("myprefix",TRUE); //will give you "myprefix" plus 23 characters to make this number unique. Set FALSE to cut it by 10 characters. Edited November 11, 2013 by dalecosp Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1457952 Share on other sites More sharing options...
keloa Posted November 12, 2013 Author Share Posted November 12, 2013 Why don't you simply use the order #ID? I need it random for some security and privacy reasons. Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1458018 Share on other sites More sharing options...
keloa Posted November 12, 2013 Author Share Posted November 12, 2013 Have you considered simply using uniqid()? $track_number = uniqid("myprefix",TRUE); //will give you "myprefix" plus 23 characters to make this number unique. Set FALSE to cut it by 10 characters. Thanks, I'll check it Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1458019 Share on other sites More sharing options...
keloa Posted November 12, 2013 Author Share Posted November 12, 2013 Have you considered simply using uniqid()? $track_number = uniqid("myprefix",TRUE); //will give you "myprefix" plus 23 characters to make this number unique. Set FALSE to cut it by 10 characters. It doesn't fit with my requirements.I think [ rand(); ] is the best choice in my case but my problem is with the loop ! Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1458020 Share on other sites More sharing options...
keloa Posted November 12, 2013 Author Share Posted November 12, 2013 Thank you guys I solved it. Quote Link to comment https://forums.phpfreaks.com/topic/283808-how-to-make-a-non-repeating-activation-code/#findComment-1458022 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.