I would REALLY like some feedback of you guys to see if i did things well and if it looks... well, solid. This has been my first actual program that can do some stuff useful
(on school we had to do $number1 = 4, $number2 = 8, echo $number1 + $numerb; <--- >.> )
So, this was an program for a client of us, wanting to make himself a lottery. Clients will get a link by the owner to my script, on which you need to enter your mail. Once you've done that, your mail address with a uniquely generated Number (lottery ) will be stored in a SQL database. The script will search for another Random number if the number already exists, and will also stop if the mail address is already stored in the database.
*Future stuff*Users will get an mail once they've singed up with their random generated number. That really isn't too hard.
Script time!
(on school we had to do $number1 = 4, $number2 = 8, echo $number1 + $numerb; <--- >.> )
So, this was an program for a client of us, wanting to make himself a lottery. Clients will get a link by the owner to my script, on which you need to enter your mail. Once you've done that, your mail address with a uniquely generated Number (lottery ) will be stored in a SQL database. The script will search for another Random number if the number already exists, and will also stop if the mail address is already stored in the database.
*Future stuff*Users will get an mail once they've singed up with their random generated number. That really isn't too hard.
Script time!
Code:
<?php
//Make connection with the server
$db = new mysqli('localhost', 'testuser14', 'supersecretpassword', 'lottery');
//check if this is a valid mail address
if(isset($_POST['mailform']) && !empty($_POST['mailform'])){
$email = mysql_real_escape_string($_POST['mailform']);
if(!mb_ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)){
// Wrong mail entered. Will give error message
print '<script type="text/javascript">';
print 'alert("Your mail address seems wrong. Please take a look at it.")';
print '</script>';
}else{
// Successful mail
//Check if the mail address is already registered in the DB
$mailcheck = mysqli_query($db, "SELECT * FROM maindb WHERE mail = '$email'");
if(mysqli_num_rows($mailcheck) > 0){
print '<script type="text/javascript">';
print 'alert("This mail is already registered!")';
print '</script>';
}else{
// Generate a random number and save it. If the generated number already exists, make a new one.
$lot1 = mt_rand(1,5300);
$check = mysqli_query($db, "SELECT * FROM maindb where lotnumber = $lot1");
if(mysqli_num_rows($check) == 0){
mysqli_query($db, "INSERT INTO maindb (lotnumber, mail) VALUES ('" . $lot1 . "','" . $email . "');");
//Send mail
}else{
$check2 = $check;
while(mysqli_num_rows($check2) >= 1){
$lot2 = mt_rand(1,5300);
$check2 = mysqli_query($db, "SELECT * FROM maindb where lotnumber = $lot2");
if(mysqli_num_rows($check2) >= 1){
//Lets the while loop repeat itself
}else{
mysqli_query($db, "INSERT INTO maindb (lotnumber, mail) VALUES ('" . $lot2 . "','" . $email . "');");
//Send mail
};
};
};
};
};
};
$db->close();
?>