Problem with $_POST, if isset() and mysql_fetch_assoc(): echo returns correct value, page source always returns else value

Questions


This question started as a different thread, but after a lot of searching and narrowing down the problem it has changed quite significantly and as such I thought it may be relevant to start a new question an link back to the old one which is:

Variable concatenated mysql query string runs fine in phpMyAdmin but not PHP in script

I don’t believe the description to be adequate anymore as the concatenation works fine.

It does seem that I am homing in on the problem. I have a simple form:

<head>
</head>
<body>
  <form method = "post" name = "testform" action = "testgenxml.php" >
    <input type = "checkbox" name = "GFCheckbox">
    <input type = "submit" value="Submit">
  </form>
</body>
</html>

I then have a PHP function that checks if GFCheckbox is selected. If it is a certain string is passed to the $query value. If it isn’t a different string is passed to the $query value. Here is the PHP function.

<?php

function parseToXML($htmlStr) 
{ 
$xmlStr=str_replace('<','&lt;',$htmlStr); 
$xmlStr=str_replace('>','&gt;',$xmlStr); 
$xmlStr=str_replace('"','&quot;',$xmlStr); 
$xmlStr=str_replace("'",'&#39;',$xmlStr); 
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
} 

$query = "";

if (isset($_POST['GFCheckbox'])) {
  $query = 'SELECT * FROM fdatav1 f INNER JOIN ddatav1 d USING(ID) WHERE (GFOption = "1")';

} else {
  $query = 'SELECT * FROM fdatav1 f INNER JOIN ddatav1 d USING(ID)';
}

$connection = mysql_connect(localhost, $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can't use db : ' . mysql_error());
}

$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

  while ($row = @mysql_fetch_assoc($result)){

    echo 'accname="' . parseToXML($row['accname']) . '" ';
    echo 'description="' . parseToXML($row['description']) . '" ';
    echo 'venue="' . parseToXML($row['venue']) . '" ';
    echo 'activity="' . parseToXML($row['activity']) . '" ';
  }
?>

When I select the checkbox, all the echo statements work perfectly and displays on screen all the records where GFOption = “1”. But when I view the page source ALL the records are echoed there. This is however specific to whatever I have in the else statement of the if condition. If the checkbox is checked or unchecked, whatever I have in the else statement executes and if there is output it gets printed to the page source.

Another interesting thing is that if I hard code my if conditional statement to

if (true)

or

if (false)

the statements work exactly the way they should, and no weird page source printing happens.

If I do

var_dump(isset($_POST['GFCheckbox'])

I get the correct boolean values depending on if GFCheckbox is checked or not.

As you can see I am running in circles with this and would really appreciate any help.

 

 

————————————————-

Answer

You appear to still be checking isset() even though the question isn’t whether it’s set, the question is whether it’s true or false. PHP munges a lot of things when you check variables as booleans but isset() on a set variable containing false will still return true.

Try trading

if (isset($_POST['GFCheckbox']))

for

if ($_POST['GFCheckbox'])

and see what happens.

forms,mysql,php,post

[], [], [], []

Facebook Comments

Website Comments

  1. rubber bracelet maker
    Reply

    Thank you a lot for sharing this with all of us you
    actually recognize what you’re talking approximately!
    Bookmarked. Please additionally consult with my web site =).
    We will have a link exchange arrangement between us

  2. www.erfworld.com
    Reply

    What i don’t understood is in truth how you are now not really much more neatly-appreciated than you may be right now.
    You’re so intelligent. You realize therefore considerably with regards to this matter, produced me in my opinion consider it
    from a lot of various angles. Its like women and men don’t seem
    to be fascinated unless it’s something to do with Girl gaga!
    Your personal stuffs great. At all times maintain it up! http://www.erfworld.com/profile/KendalOaks

  3. custom made wristbands
    Reply

    Excellent post. Keep writing such kind of information on your page.

    Im really impressed by your blog.[X-N-E-W-L-I-N-S-P-I-N-X]Hello there, You’ve performed an excellent job.
    I’ll certainly digg it and personally recommend to my friends.
    I’m confident they will be benefited from this web site.

  4. custom wristbands
    Reply

    First of all I would like to say fantastic blog! I had
    a quick question which I’d like to ask if you don’t mind.
    I was interested to know how you center yourself and clear your thoughts before writing.
    I have had a tough time clearing my thoughts in getting
    my ideas out. I truly do enjoy writing however it just seems like the
    first 10 to 15 minutes tend to be lost simply just trying to figure out
    how to begin. Any suggestions or hints? Cheers!

Post a comment