Jump to content

simple script... or so i thought


toryadams

Recommended Posts

There is a sql query happening if the item is not empty, what I NEED and can not seem to figure out is to re run the rand() function. The server is PHP 4 so it is not possible to use the goto statement... That would be to easy. So any help would be great... Here is the code... the script running after the script finds a empty sql row is ot included... it works... just this one part does not.

 

<?php
$connection = mysql_connect(CREDENTIALS REMOVED) or die(mysql_error());
  	mysql_select_db("testingblock", $connection)or die(mysql_error());

  	
  	$random_num = rand(1,10); 
  	
if($random_num == 1){
  		$query = sprintf("SELECT email FROM access_codes WHERE id='1'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');


			}
			else {
				echo $recipient; 
			}
}
else if ($random_num == 2){
  		$query = sprintf("SELECT email FROM access_codes WHERE id='2'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result); 

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
}
  	else if ($random_num == 3){
  		$query = sprintf("SELECT email FROM access_codes WHERE id='3'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
  	}
  	else if ($random_num == 4){ 
  		$query = sprintf("SELECT email FROM access_codes WHERE id='4'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);

		echo '<br />'; 

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
  	}
  	else if ($random_num == 5){ 
  		$query = sprintf("SELECT email FROM access_codes WHERE id='5'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);


			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
  	}
  	else if ($random_num == 6){
  		$query = sprintf("SELECT email FROM access_codes WHERE id='6'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);



			if(!empty($row['email'])){

				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
  	}
  	else if($random_num == 7){
  		$query = sprintf("SELECT email FROM access_codes WHERE id='7'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
  	}
  	else if ($random_num == { 
  		$query = sprintf("SELECT email FROM access_codes WHERE id='8'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');

			}
			else {
echo $recipient; 
			}
  	}
  	else if ($random_num == 9){
  		$query = sprintf("SELECT email FROM access_codes WHERE id='9'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);

		echo '<br />'; 

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
  	}
  	else if ($random_num == 10){ 
  		$query = sprintf("SELECT email FROM access_codes WHERE id='10'", 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result);

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
  	}
  

?>


[icode]

Link to comment
https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/
Share on other sites

why not ditch all the 'if' statements and just do this?

if (isset($random_num)){
  		$query = sprintf("SELECT email FROM access_codes WHERE id='".$random_num."'"); 
	mysql_real_escape_string($email)); 

	$result = mysql_query($query); 
	$row = mysql_fetch_assoc($result); 

			if(!empty($row['email'])){
				header('Location: http://www.thestreetbeatint.com/Testing/blah.php');
			}
			else {
echo $recipient;
			}
}

That definetly consolidated the coding I never thought to use the isset as a way to go. But just ran it... the main problem still remains. I need the variable 'random_num = rand(1,10)' to keep running and checking the db until an empty cell is returned... The header location idea I had will not work keeps giving me an error. would a simple while loop work for this? or a do while?

ok, I have a database with codes in it.

A form with an email input area.

 

From the from I will obtain the users email. Where I then will send the user a email with the code.

Each code can only be used once.

so I am storing each correlating email with the code that was sent to it.

 

so if the cell for emails is not empty it needs to then regenerate a random number and select another row with a empty email field.

 

the random number is so it is not predictable.

bit of a problem with your logic there.

so if the cell for emails is not empty it needs to then regenerate a random number and select another row with a empty email field.

this makes no sense.

 

You need to...

SELECT code FROM access_codes WHERE email='$_POST['email']'

I went ahead and implemented the while statement i thought of and using your isset idea... and it works so it seems. I am going to push it to its limits a little a bit. And post back and let you know.... Thank you for the help... Moderators PLEASE DO NOT CHANGE THIS TO SOLVED JUST YET. i beg you... thank you.

Hey Masterace thanks for sending me on the right path... by your isset consolidation of what I had... it got my 'head gears' turning again. lol. The while statement was perfect for what I needed it to do. IDK if I explained enough. But I did want to thank you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.