Jump to content

How to make a non-repeating activation code


keloa
Go to solution Solved by dalecosp,

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 20
done
Your tracking number is 10175

 

The problems are :

1-The error on line 20

2-the data didn't enter the database

Link to comment
Share on other sites

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 !

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.