RevCMS lottery script

Status
Not open for further replies.
Newbie Spellweaver
Joined
Oct 27, 2011
Messages
7
Reaction score
4
Hey guys,
I've seen some hotels have a lottery, so here you go

Lottery.php
PHP:
<?php 
    $Query = mysql_query("SELECT * FROM lottery WHERE username = '". $_SESSION['user']['username'] ."'");
    
    IF($profile = mysql_fetch_array($Query)){
        $Figure_1 = mysql_real_escape_string($profile['Figure_1']);
        $Figure_2 = mysql_real_escape_string($profile['Figure_2']);
        $Figure_3 = mysql_real_escape_string($profile['Figure_3']);
        ?>
<p>You have already entered!</p>
	<select>
		<option><?php echo $Figure_1; ?></option>
	</select>
	<select>
		<option><?php echo $Figure_2; ?></option>
	</select>
	<select>
		<option><?php echo $Figure_3; ?></option>
	</select>
<?php  } else { ?>
										<form action="lottery2" method="post"><div style="color:#D8000C; background-color:#FFBABA; border:1px solid #000000; padding:2px;"/>NOTE: This will cost you <u>5000</u> coins</div><br>
											<select id="Figure_1" name="Figure_1">
												<option value="1">1</option>
												<option value="2">2</option>
												<option value="3">3</option>
												<option value="4">4</option>
												<option value="5">5</option>
												<option value="6">6</option>
											</select>
											<select id="Figure_2" name="Figure_2">
												<option value="1">1</option>
												<option value="2">2</option>
												<option value="3">3</option>
												<option value="4">4</option>
												<option value="5">5</option>
												<option value="6">6</option>
											</select>
											<select id="Figure_3" name="Figure_3">
												<option value="1">1</option>
												<option value="2">2</option>
												<option value="3">3</option>
												<option value="4">4</option>
												<option value="5">5</option>
												<option value="6">6</option>
											</select><br><br>
											<input type="submit" id="submit" value="Submit" />
										</form>
<?php  } ?>


Lottery2.php
PHP:
##->Inserting values in database

$cost = '5000'; // How much each lottery ticket will cost

	// Grabbing the POST'ed figures
    $Figure_1 = mysql_real_escape_string($_POST["Figure_1"]);
    $Figure_2 = mysql_real_escape_string($_POST["Figure_2"]);
    $Figure_3 = mysql_real_escape_string($_POST["Figure_3"]);
	$userCoins = "" . $_SESSION['user']['credits'] . "";

	// If users coins are below the lottery cost, we'll send the header as an error
	if($userCoins < $cost)
		{
			Header("Location: me?Error?=Balance");
		}
		else {
		
		// Taking away the cost of lottery to the users account
		$coins = mysql_query("UPDATE `users` SET `credits` = credits - ". $cost ." WHERE username = '" . $_SESSION['user']['username'] . "'");

		// Inserting the values into database, we'll send the header as successful
	     $sql = mysql_query("INSERT INTO `lottery` (Username, Figure_1, Figure_2, Figure_3) VALUES ('". $_SESSION['user']['username'] ."', '". $Figure_1 . "', '". $Figure_2 . "', '". $Figure_3 .")");
		
			Header("Location: me?successful");

			}

Lottery.sql
Code:
-- ----------------------------
-- Table structure for `lottery`
-- ----------------------------
DROP TABLE IF EXISTS `lottery`;
CREATE TABLE `lottery` (
  `Username` varchar(50) NOT NULL,
  `Figure_1` varchar(1) NOT NULL,
  `Figure_2` varchar(1) NOT NULL,
  `Figure_3` varchar(1) NOT NULL,
  PRIMARY KEY (`Username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ------------------------

Place this as a page in your housekeeping or any private file no one knows about
PHP:
<?php
// MySQL connection
$host = "localhost";
$user = "root";
$pass = "root";
$data = "rev_phoenix";

$connection = mysql_connect($host,$user,$pass) 
	or die("Could not connect: ".mysql_error());
mysql_select_db($data,$connection) 
or die("Error in selecting the database:".mysql_error());

$prize = "5000"; // Prize

$Query1 = mysql_Query("SELECT * FROM lottery ORDER BY RAND() LIMIT 1");
    
    IF($profile = mysql_fetch_array($Query1)){
        $username = mysql_real_escape_string($profile['Username']);
        $Figure_1 = mysql_real_escape_string($profile['Figure_1']);
        $Figure_2 = mysql_real_escape_string($profile['Figure_2']);
        $Figure_3 = mysql_real_escape_string($profile['Figure_3']);


	$sql1 = mysql_Query("UPDATE `users` SET `credits` = credits + ".$prize." WHERE username = '".$username."'");
		echo "<div style='padding:10px;background-color:green;border-radius:3px;font-family:arial;font-size:12px;color:#FFF;text-align:center;'><b>" . $username . "</b>   " . $Figure_1 . " - " . $Figure_2 . " - " . $Figure_3 . " Has been awarded ". $prize . " coins!</div>";
$empty = mysql_query ("truncate table `lottery`"); // Emptying the table
	
}

else {

echo "<div style='padding:10px;background-color:green;border-radius:3px;font-family:arial;font-size:12px;color:#FFF;text-align:center;'>Error no entry in database!</div>";
	}
?>

Once you visit this page it will
-> Select a random row to win
-> Give the user x amount of credits
-> Empty the table

Play around with the pages, most likely visit the page once a week?
With Lottery.php you can just put it in a div container. Find any errors PM me
Sorry if this is already released anyways.. thanks.
 
Last edited:
How can you make someone win? or does it go automaticly?

Nice question, well it goes into your database and you could pick it randomly. But forget all that if you want me to quickly code something when the submit button is active:
-> Get a random row
-> Send user x amount of credits
-> Empty the table?
 
Last edited:
Optimize your querys. They're very badly done. Anways it's nice for those who use revcms, i prefer own bullshit.

merkleman - RevCMS lottery script - RaGEZONE Forums


Anyway why did you put this:
Code:
mysql_query($coins,$sql);

In the code?

It's useless and $coins and $sql are both a query resource and not a string or link.
 
Why did you do this?
Code:
        // Inserting the values into database, we'll send the header as successful
        $sql = mysql_query("INSERT INTO `lottery` SET
                `Username` = '" . $_SESSION['user']['username'] . "', 
                `Figure_1` = '".$Figure_1."', 
                `Figure_2` = '".$Figure_2."', 
                `Figure_3` = '".$Figure_3."' ");
You dont insert a row with set values..
You do:
Code:
INSERT INTO `lottery` (`Username`, `Figure_1`, `Figure_2`, `Figure_3`) VALUES ('". mysql_real_escape_string($_SESSION['user']['username']) ."', '" . $Figure_1 . "', '" . $Figure_2 . "', '" . $Figure_3 . "');
 
Why did you do this?
Code:
        // Inserting the values into database, we'll send the header as successful
        $sql = mysql_query("INSERT INTO `lottery` SET
                `Username` = '" . $_SESSION['user']['username'] . "', 
                `Figure_1` = '".$Figure_1."', 
                `Figure_2` = '".$Figure_2."', 
                `Figure_3` = '".$Figure_3."' ");
You dont insert a row with set values..
You do:
Code:
INSERT INTO `lottery` (`Username`, `Figure_1`, `Figure_2`, `Figure_3`) VALUES ('". mysql_real_escape_string($_SESSION['user']['username']) ."', '" . $Figure_1 . "', '" . $Figure_2 . "', '" . $Figure_3 . "');

Damn nice spotting, added a new page. Thanks btw. :tongue:
 
Using $_SESSION['user']['credits'] is a horrible idea, because the sessions credits will always stay the same, you can purchase as many tickets as you like using that.
 
Nice, but why is your username stored in the database? What if someone were to buy a 'winning' ticket but before the draw decided to change their name? Maybe store their ID instead? xD

True never really thought about it, however it's very easy to change from username to id. haha:)
Oh yeah, about the $_SESSION coins; it has never happened to me that the coins stay the same it updates & just saying the user only gets 1 lottery ticket unless the whole table is emptied / winner is chosen.

Thanks for the replies everyone,
 
To make life easier you could fully automate it by setting a cronjob.
 
My lottery2 shows
##->Inserting values in database $cost = '5000'; // How much each lottery ticket will cost // Grabbing the POST'ed figures $Figure_1 = mysql_real_escape_string($_POST["Figure_1"]); $Figure_2 = mysql_real_escape_string($_POST["Figure_2"]); $Figure_3 = mysql_real_escape_string($_POST["Figure_3"]); $userCoins = "" . $_SESSION['user']['credits'] . ""; // If users coins are below the lottery cost, we'll send the header as an error if($userCoins < $cost) { Header("Location: me?Error?=Balance"); } else { // Taking away the cost of lottery to the users account $coins = mysql_query("UPDATE `users` SET `credits` = credits - ". $cost ." WHERE username = '" . $_SESSION['user']['username'] . "'"); // Inserting the values into database, we'll send the header as successful $sql = mysql_query("INSERT INTO `lottery` (Username, Figure_1, Figure_2, Figure_3) VALUES ('". $_SESSION['user']['username'] ."', '". $Figure_1 . "', '". $Figure_2 . "', '". $Figure_3 .")"); Header("Location: me?successful"); }
 
Status
Not open for further replies.
Back