简体   繁体   中英

Variable always returns the word “Null”

The following code executes query1 if the 'Less than 16' checkbox is checked and executes query2 if the '16 or more' checkbox is checked. This works perfectly.

<?php
//error_reporting (E_ALL ^ E_NOTICE);
$conn = mysql_connect('localhost','student','student') or die(mysql_error());
mysql_select_db('vgs',$conn);

//Get Question 1
if (isset($_GET['q1option'])) 
{
    $q1option = $_GET['q1option'];
} 
else 
{
    $q1option = "Null";
}

echo("".$_GET['q1option']);
echo("".$q1option);

//Process Question 1
if ($q1option == "Less than 16") 
{
    $query1 = "UPDATE free_hours SET times_selected=times_selected+1 WHERE q1option='Less than 16'";
    $result1 = mysql_query($query1,$conn) or die(mysql_error());
}
if ($q1option == "16 or more") 
{
    $query2 = "UPDATE free_hours SET times_selected=times_selected+1 WHERE q1option='16 or more'";
    $result2 = mysql_query($query2,$conn) or die(mysql_error());
}

However, I get the following error when I echo $_GET['q1option'].

"Notice: Undefined index: q1option in C:\\wamp\\www\\Student\\vgs\\process_answers.php on line 16"

Line 16 is this:

echo("".$_GET['q1option']);

Also, when I echo $q1option it always echos the word "Null" even if Less than 16 is checked and the 'times_selected' value is incrementing.

What is the problem here?

Thanks for any help.

Daniel

The problem is that you are not receiving a GET parameter called q1option . Check your client-side code with debugger and make sure it's being sent.

You can see what you are receiving server-side by doing something like:

error_log('$_GET: '.print_r($_GET, true));

(alternatively you can echo it out if you're in a safe environment).

first: wrap

echo("".$_GET['q1option']);

into

    if(isset($_GET['q1option'])){
      echo("".$_GET['q1option']);
    }

to get rid of notice, then if your $_GET doesn't hold a thing, make sure you use or to submit your form, and you don't accidentally trying to put two forms and submit another one ;). Whole code (form file + process_answers.php) would help here, that's for sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM