开发者

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

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']) . '" ';
   开发者_Go百科 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.


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.


I can't see anything in that code suggesting it would output all records when submitted with the box checked. It's probably the case that when you view the page source it's not sending the POST data, so you get all the records back.

If you have the Web Developer Toolbar for Firefox, you can verify this by using View Source -> View Generated Source, or alternatively with Firebug by inspecting the DOM. Failing that, try viewing source using a different web browser.


Here: http://jfcoder.com/test/checkbox.php

I have:

<html>
<head>
</head>
<body>
<pre>
<?php 
print_r($_POST);

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

echo "\n\nQUERY: $query";

?>
</pre>
  <form method = "post" name = "testform" action="" >
    <input type = "checkbox" name = "GFCheckbox">
    <input type = "submit" value="Submit">
  </form>
</body>
</html>

And it's acting as you're describing you want it to. Specifically, to use a WHERE clause if the checkbox is selected on the form submit.

So I don't think that it's the if statement. If you're using MySQL, your query looks wrong.

NOTE: I think most/all (see third note) relational database systems use the single quote syntax, but I'm not sure.

SECOND NOTE: I'm actually looking for the documentation to see if this is true. I've certainly always thought single quotes were required.

THIRD NOTE: This is actually NOT a problem with MySQL, see When is it necessary to escape double quotes and other characters when inserting them into MySQL database? for more information.

$query = '
SELECT * 
FROM fdatav1 f 
INNER JOIN ddatav1 d USING(ID) 
WHERE (GFOption = "1")
';

You need to use single quotes on the WHERE check, not double quotes. I would stick with single quotes for consistency, but apparently it's not required by MySQL.

$query = '
SELECT * 
FROM fdatav1 f 
INNER JOIN ddatav1 d USING(ID) 
WHERE GFOption = \'1\'
';

Or:

$query = "
SELECT * 
FROM fdatav1 f 
INNER JOIN ddatav1 d USING(ID) 
WHERE GFOption = '1'
";

I believe this is probably why your results are not working as expected.

Also, IMO you should not use isset() for the if but should check for the actual status report; ie...

if ($_POST['GFCheckbox'] == 'on') {

And I might do this a little differently (see comment):

$WHERE = "";

if (isset($_POST['GFCheckbox'])) {
    if ($WHERE == '') {
        $WHERE = "WHERE ";
    }
    $WHERE = "GFOption = '1'";
}

// This way it's easier to add options
// and edit your base query later.
// For instance, in case you add a 
// GROUP, LIMIT or ORDER BY.
if (isset($_POST['ABCCheckbox'])) {
    if ($WHERE == '') {
        $WHERE = "WHERE ";
    } else {
        $WHERE = " AND ";
    }
    $WHERE = "ABCOption = 'efg'";
}

$query = "
SELECT * 
FROM fdatav1 f 
INNER JOIN ddatav1 d USING(ID)
$WHERE
";


Use $_GET or $_SESSION variables

The problem boiled down to Google Chrome. The issue I was having was a recognized bug in the software: http://code.google.com/p/chromium/issues/detail?id=523 which I believe they are fixing in future editions of Google Chrome. I have just update mine and still does not work correctly.

I switched browsers to Firefox and Explorer and they rendered everthing correctly, even the XML output I was after.

Credit for this solution goes out to Roe for pointing out the potenital browser problem.

According to other posts using $_GET in stead of $_POST should work fine. $_SESSION variables should also do the trick.


If checkbox not checked, then $_POST['GFCheckbox'] = 0, else $_POST['GFCheckbox'] = 1

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜