toryadams Posted August 25, 2011 Share Posted August 25, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/ Share on other sites More sharing options...
Pikachu2000 Posted August 25, 2011 Share Posted August 25, 2011 I've removed your database connection credentials, but you should change your password nonetheless. Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261665 Share on other sites More sharing options...
toryadams Posted August 25, 2011 Author Share Posted August 25, 2011 Thanks I just caught it, and yes I know this is a test server I paid for. Thanks either way Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261667 Share on other sites More sharing options...
toryadams Posted August 25, 2011 Author Share Posted August 25, 2011 Should I in someway be wrapping any of this code in a loop? That is the only thing I can think of. I just don't know where to begin on that part of it, if that is the answer. Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261727 Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261733 Share on other sites More sharing options...
toryadams Posted August 25, 2011 Author Share Posted August 25, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261737 Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 wait wait wait... go back to the start, forget your current script. What are you trying achieve? I'm certain you're overcomplicating a simple problem. Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261739 Share on other sites More sharing options...
toryadams Posted August 25, 2011 Author Share Posted August 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261744 Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 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']' Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261751 Share on other sites More sharing options...
toryadams Posted August 25, 2011 Author Share Posted August 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261752 Share on other sites More sharing options...
toryadams Posted August 25, 2011 Author Share Posted August 25, 2011 I did not think of doing that... i had the while loop, looping until it found a empty spot, and then had the script send an email to the email provided, then updated to that spot... if that makes since. Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261754 Share on other sites More sharing options...
toryadams Posted August 25, 2011 Author Share Posted August 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261766 Share on other sites More sharing options...
MasterACE14 Posted August 25, 2011 Share Posted August 25, 2011 not a problem mate, practice makes perfect Quote Link to comment https://forums.phpfreaks.com/topic/245650-simple-script-or-so-i-thought/#findComment-1261767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.