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
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;
			}
}

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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']'

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.