Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,196 members, 7,953,712 topics. Date: Friday, 20 September 2024 at 12:14 AM

Php/mysql Pin And Serial Number Generation Script - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Php/mysql Pin And Serial Number Generation Script (9197 Views)

I Need Internet Download Manager Serial Number. / True Random Number Generation - Fun / Php And Mysql Pin And Serial Number Generation Script (2) (3) (4)

(1) (Reply) (Go Down)

Php/mysql Pin And Serial Number Generation Script by uchdollar111: 5:57pm On Nov 30, 2012
Fellow programmers, I greet you all. I'm currently working on a school assignment and was asked to create a system that generates 14 digits pin codes at random together with their unique serial numbers. The system will make sure that thesame pin is not generated twice. The idea is to create pin numbers for customers when the customer purchases the pin and redeems it, that pin can never be used again. Am using php and mysql. Please help as I am stuck right now.
Re: Php/mysql Pin And Serial Number Generation Script by Chimanet(m): 6:40pm On Nov 30, 2012
so far wat have u done? we need to see your effort, remember Nairaland Programmers forum is not a codemill grin
Re: Php/mysql Pin And Serial Number Generation Script by uchdollar111: 8:16pm On Nov 30, 2012
I'm really new to programming and am just starting up so i need suggestions on where to start
Re: Php/mysql Pin And Serial Number Generation Script by BigStar1(m): 1:03pm On Dec 01, 2012
Using time() function will give you a 10 digit serial number. To ensure uniqueness, save the first number in a table then check the next one that was generated against the table. Loop untill you got zero record when you run a select query against the table where sn = currentno. If that is true, then insert it also. Loop will do this for you. I've done it before but am not on my laptop. I would have posted the query.
Re: Php/mysql Pin And Serial Number Generation Script by hoodboi(m): 2:49pm On Dec 01, 2012
D query simply is select sn,pin from tablename where pin = (d value u wnt to check).. Since d result set will also return a sn u can check the sn against the result set too to make sure. But I believe once the pin has been found i.e ur result set returning true then no need to check further
Re: Php/mysql Pin And Serial Number Generation Script by uchdollar111: 4:55pm On Dec 01, 2012
Thanks guys for contributions. But I still need more help.
Re: Php/mysql Pin And Serial Number Generation Script by xyluz: 8:22pm On Dec 01, 2012
It actually depends on the language you wish to use, either PHP, javascript, JQuery, java etc... this would determine how you would do it. and for each language there are several ways yo achieve random number generation. For example, javascript: you can use the math.random(), for PHP: you can use rand(), or for both you could just create an array containing numbers 0 to 9, create a code that randomly sort through the array and output the result. Its even possible to have different functions working together in the background and outputting the values together (this is more secured). You could as well use PHP in conjunction with SQL to generate your random numbers.

So it all depends on the language you want to use. What language do you wish to use?
Re: Php/mysql Pin And Serial Number Generation Script by olyjosh(m): 8:58pm On Dec 01, 2012
Any way no body is ready to spoon-feed u with codes but let me help u out with d the little part living the rest for you to complete


<?php
/**************************************************
***************************************************
*** author : Joshua Aroke(oljursh) ***
***************************************************
**************************************************/

$connection = mysql_connect("localhost","root",""wink;
if(!$connection){
die("database connection failed: ". mysql_error());
}

$db_select = mysql_select_db("pin",$connection);
if(!$db_select){
die("error selecting from d database"wink;
}
for ($index = 0; $index < 100; $index++) {
$rand1 = rand(1000000, 9999999); $rand2 = rand(1000000, 9999999);
$pin = $rand1.$rand2;

$check = mysql_query("SELECT * FROM pin WHERE pin='$pin'"wink;
if(mysql_num_rows($check)>0){
$index-=1;
}elseif (mysql_num_rows($check)==0) {
mysql_query("INSERT INTO pin (pin) values ($pin)"wink;
$sn = mysql_query("SELECT serial FROM pin WHERE pin='$pin'"wink;
while ($row1 =mysql_fetch_array($sn)) {
$pinSerial = $row1["serial"];
}
echo"pin: " .$pin ." serials: ".$pinSerial."\n";
}
}
?>




DATABASE INFOS
host is "localhost"
database name is "pin"
table name is "pin"
username is "root"
no password


Use this info to create database and table

The above script will generate 100 unique pins randomly with serial no for you...
It will also check such dat no pin exist in the database twice.

I will leave the rest for you to manipulate since you said you are into programming

Take note of the loop above for your manipulation of codes. Hence you can pass parameter from your GUI interface(html page) to represent 100 in that loop.
I expect you to know how to write script that will used the pin and render it status "used" in the database

Below is a screen capture of the result from browser and database



Get back to me if you need more help. But note that I wont like to feed you with code that i xpect you to write ur self.

2 Likes

Re: Php/mysql Pin And Serial Number Generation Script by Nobody: 9:17pm On Dec 01, 2012
why not use int values 1 and 0 to indicate status instead of the text "valid"

the logic may halt the script sooner than expected, adding time() to the mix wont hurt.
nice contribution.

1 Like

Re: Php/mysql Pin And Serial Number Generation Script by xyluz: 12:50am On Dec 02, 2012
Yes indeed that'll work, nice work olyjosh... i agree with webdezzi though, int values is a more professional way to do it, or simple varchar such as A and B or N and Y..
Re: Php/mysql Pin And Serial Number Generation Script by olyjosh(m): 3:21pm On Dec 02, 2012
Special thanks to webdezzi and xylus for ur conbutions. I Overstand what u guys said about usin boolean values for d status column. I m only tryin to consider d young programmer who posted dis question so dat he can understand d concept easily and a case where status value of a pin can exist in three form such as valid or used or blocked.

Thanks very much for ur contributions, they are both welcome
Re: Php/mysql Pin And Serial Number Generation Script by xyluz: 6:10pm On Dec 02, 2012
You're welcome... Maybe we can do somethings together later.
Re: Php/mysql Pin And Serial Number Generation Script by Nobody: 6:37pm On Dec 02, 2012
you are welcome.
incases where we have 3 values, then 0, 1, 2 will work.
int takes up less space

when the count goes to say a million,
1 in a million place is 1 million bytes which is 977 kilobytes

thats 4.9 Megs if you had used "valid" instead.
Re: Php/mysql Pin And Serial Number Generation Script by trusted1: 5:46am On Aug 17, 2013
Thanks a million for this, I was able to understand and manipulate this to my taste. Groovie!!!
Re: Php/mysql Pin And Serial Number Generation Script by nnamdiosu(m): 10:34am On Jan 21, 2015
olyjosh:
Any way no body is ready to spoon-feed u with codes but let me help u out with d the little part living the rest for you to complete


<?php
/**************************************************
***************************************************
*** author : Joshua Aroke(oljursh) ***
***************************************************
**************************************************/

$connection = mysql_connect("localhost","root",""wink;
if(!$connection){
die("database connection failed: ". mysql_error());
}

$db_select = mysql_select_db("pin",$connection);
if(!$db_select){
die("error selecting from d database"wink;
}
for ($index = 0; $index < 100; $index++) {
$rand1 = rand(1000000, 9999999); $rand2 = rand(1000000, 9999999);
$pin = $rand1.$rand2;

$check = mysql_query("SELECT * FROM pin WHERE pin='$pin'"wink;
if(mysql_num_rows($check)>0){
$index-=1;
}elseif (mysql_num_rows($check)==0) {
mysql_query("INSERT INTO pin (pin) values ($pin)"wink;
$sn = mysql_query("SELECT serial FROM pin WHERE pin='$pin'"wink;
while ($row1 =mysql_fetch_array($sn)) {
$pinSerial = $row1["serial"];
}
echo"pin: " .$pin ." serials: ".$pinSerial."\n";
}
}
?>




DATABASE INFOS
host is "localhost"
database name is "pin"
table name is "pin"
username is "root"
no password


Use this info to create database and table

The above script will generate 100 unique pins randomly with serial no for you...
It will also check such dat no pin exist in the database twice.

I will leave the rest for you to manipulate since you said you are into programming

Take note of the loop above for your manipulation of codes. Hence you can pass parameter from your GUI interface(html page) to represent 100 in that loop.
I expect you to know how to write script that will used the pin and render it status "used" in the database

Below is a screen capture of the result from browser and database



Get back to me if you need more help. But note that I wont like to feed you with code that i xpect you to write ur self.

God bless u for posting this script. many thanks to you

2 Likes

Re: Php/mysql Pin And Serial Number Generation Script by guru01(m): 3:48pm On Jan 21, 2015
why not use int values 1 and 0 to indicate status instead of the text "valid"

the logic may halt the script sooner than expected, adding time() to the mix wont hurt.
nice contribution.
what is wrong with using characters?
must everybody code like you?
Re: Php/mysql Pin And Serial Number Generation Script by Nobody: 10:09pm On Jan 21, 2015
guru01:

what is wrong with using characters?
must everybody code like you?
Like he said, Its a Megabyte(MB) hack... Lol...
Re: Php/mysql Pin And Serial Number Generation Script by guru01(m): 11:02pm On Jan 21, 2015
Djangocode:

Like he said, Its a Megabyte(MB) hack... Lol...
hack ni hack kor.
Re: Php/mysql Pin And Serial Number Generation Script by uchdollar111: 10:37pm On Mar 29, 2015
I was just going through this today. Those days though! I can even code this script now if you wake me up from sleep in the middle of the night.
Re: Php/mysql Pin And Serial Number Generation Script by Nobody: 10:12am On Mar 30, 2015
olyjosh:
Any way no body is ready to spoon-feed u with codes but let me help u out with d the little part living the rest for you to complete


<?php
/**************************************************
***************************************************
*** author : Joshua Aroke(oljursh) ***
***************************************************
**************************************************/

$connection = mysql_connect("localhost","root",""wink;
if(!$connection){
die("database connection failed: ". mysql_error());
}

$db_select = mysql_select_db("pin",$connection);
if(!$db_select){
die("error selecting from d database"wink;
}
for ($index = 0; $index < 100; $index++) {
$rand1 = rand(1000000, 9999999); $rand2 = rand(1000000, 9999999);
$pin = $rand1.$rand2;

$check = mysql_query("SELECT * FROM pin WHERE pin='$pin'"wink;
if(mysql_num_rows($check)>0){
$index-=1;
}elseif (mysql_num_rows($check)==0) {
mysql_query("INSERT INTO pin (pin) values ($pin)"wink;
$sn = mysql_query("SELECT serial FROM pin WHERE pin='$pin'"wink;
while ($row1 =mysql_fetch_array($sn)) {
$pinSerial = $row1["serial"];
}
echo"pin: " .$pin ." serials: ".$pinSerial."\n";
}
}
?>




DATABASE INFOS
host is "localhost"
database name is "pin"
table name is "pin"
username is "root"
no password


Use this info to create database and table

The above script will generate 100 unique pins randomly with serial no for you...
It will also check such dat no pin exist in the database twice.

I will leave the rest for you to manipulate since you said you are into programming

Take note of the loop above for your manipulation of codes. Hence you can pass parameter from your GUI interface(html page) to represent 100 in that loop.
I expect you to know how to write script that will used the pin and render it status "used" in the database

Below is a screen capture of the result from browser and database



Get back to me if you need more help. But note that I wont like to feed you with code that i xpect you to write ur self.

Nice code, I most say I learnt from it, but I think if u go with the instructions of the OP, the lecturer said they should generate a random pin that will never generate again. Your code is excellent, but in this case database should not be required. When I was still in school I remember a lecturer gave this exact same assignment, many students did what you did, but He the lecturer failed everyone saying databases should not be used and that the pin generation, should be on the PC or Server the script was been installed in.

OP depending on the language u want to use, check the programming language documentation with emphasis on the RAND function.

I bliv u r familiar with PHP, so search for mt_rand function.

I'm not on PC would have tested some code for you.

(1) (Reply)

Ccna Certification / Which Phone Will You Recommend For A Programmer? / What Programming Language Is Used In Building PES?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 44
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.