YMYL Posted January 4, 2013 Share Posted January 4, 2013 Hi all I'm having trouble with the code displayed below. The output I get after running it is answer1yesanswer2yesanswer3yesanswer5yesanswer6yesanswer7yes when I run it without any filler for the $answer variables. My question to you is 1) Why isset not working? 2) Why does it go 1-2-3-5-6-7 (skip 4 and ? Thank you for your time. --------- <?php require_once('functions.php'); session_start(); check_valid_user(); $question = $_POST['question']; $answer1 = $_POST['answer1']; $answer2 = $_POST['answer2']; $answer3 = $_POST['answer3']; $answer4 = $_POST['answer4']; $answer5 = $_POST['answer5']; $answer6 = $_POST['answer6']; $answer7 = $_POST['answer7']; $answer8 = $_POST['answer8']; $user = $_SESSION['valid_user']; $conn = db_connect(); //if (!$conn->query("insert into polls values (DEFAULT, '$question', '$user')")) //throw new Exception('Bookmark could not be inserted.'); //$insertid = mysqli_insert_id($conn); for ($i = 1; $i < 9; $i++) { $a = answer; $b = $a.$i; $c = $$b; if(isset($$B)) { echo $b; echo 'yes'; //if (!$conn->query("insert into votes values (DEFAULT, '$insertid', '$c',0)")) //throw new Exception('vote could not be inserted.'); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/272672-code-problem/ Share on other sites More sharing options...
YMYL Posted January 4, 2013 Author Share Posted January 4, 2013 OK this is very odd. I just added else{echo 'no';} And it looks like 1) isset is properly detecting that the variables aren't set 2) #4 and #8 are properly accounted for in the loop. Any idea why this might be? Quote Link to comment https://forums.phpfreaks.com/topic/272672-code-problem/#findComment-1403113 Share on other sites More sharing options...
requinix Posted January 4, 2013 Share Posted January 4, 2013 A moot question because your code uses variable variables and that is a problem. Change your form to name the answers' inputs like <whatever those things are name="answer[1]" blah blah blah> Then $_POST["answer"] will be a very convenient array structure for you to look at. Obviates most of the code you have there now. Quote Link to comment https://forums.phpfreaks.com/topic/272672-code-problem/#findComment-1403115 Share on other sites More sharing options...
cpd Posted January 4, 2013 Share Posted January 4, 2013 my eyes hurt. Why would you not use an array for your answers as suggested by requinix. You can't get a more perfect example where you should use an array. Cycle through the array with a foreach loop and raise a flag when something is empty - probably 1/3 the code you have there and way more efficient. Quote Link to comment https://forums.phpfreaks.com/topic/272672-code-problem/#findComment-1403150 Share on other sites More sharing options...
White_Lily Posted January 4, 2013 Share Posted January 4, 2013 When i write forms, and need to check for empty fields, I use this code for it: $validateArray = array("name" => "text", "email" => "email"); foreach($validateArray as $key => $value) { if($value == "text") $($key) = htmlspecialchars(mysql_real_escape_string($_POST[$key])); else $$key = mysql_real_escape_string($_POST[$key]); if(empty($$key)) { } } Quote Link to comment https://forums.phpfreaks.com/topic/272672-code-problem/#findComment-1403160 Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 White_Lily: I would not recommend that code for use by anyone. You included. It's just plain wrong, at all levels. It doesn't validate anything; The output escaping is arbitrary, and done at the completely wrong place; It uses completely unnecessary variable variables, which (as stated above) is nothing but harmful for your code; And it has absolutely nothing to do with the problem the OP posted about. I strongly recommend deleting that code, learning about proper validation, as well as when to use output escaping (and how), and starting from scratch. Do note that input validation is not something that has to be tailored to every single piece of input data, and cannot be applied as a "one size fits all" mass operation. Quote Link to comment https://forums.phpfreaks.com/topic/272672-code-problem/#findComment-1403999 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.