Jump to content

code problem


YMYL

Recommended Posts

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 8)?

 

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.');

}

}

?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

:o 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.

Link to comment
Share on other sites

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))
{

}
}

Link to comment
Share on other sites

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.

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.