$ _POST问题,如果isset()和mysql_fetch_assoc():echo返回正确的值,页面源始终返回其他值 [英] Problem with $_POST, if isset() and mysql_fetch_assoc(): echo returns correct value, page source always returns else value

查看:94
本文介绍了$ _POST问题,如果isset()和mysql_fetch_assoc():echo返回正确的值,页面源始终返回其他值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是从另一个线索开始的,但是经过大量搜索和缩小问题之后,这个问题已经发生了很大的变化,因此我认为开始一个新的问题可能是相关的,即回到旧问题:



变量级联的mysql查询字符串在phpMyAdmin中运行正常,但脚本中没有PHP

我不相信说明已经足够了,因为连接工作正常。



似乎我正在解决这个问题。我有一个简单的表单:

 < head> 
< / head>
< body>
< form method =postname =testformaction =testgenxml.php>
< input type =checkboxname =GFCheckbox>
< input type =submitvalue =提交>
< / form>
< / body>
< / html>

然后我有一个PHP函数来检查GFCheckbox是否被选中。如果某个字符串被传递给$ query值。如果不是将不同的字符串传递给$ query值。这是PHP函数。

 <?php 

函数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 = ;

如果(isset($ _ POST [ 'GFCheckbox'])){
$查询=SELECT * FROM fdatav1˚FINNER JOIN ddatav1 d USING(ID),其中(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($数据库,$连接);
如果{
模( 'Can\'t使用分贝:' mysql_error()。)($ db_selected!) ;
}

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

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

ech o'accname ='。 parseToXML($ row ['accname'])。 ''';
echo'description ='。 parseToXML($ row ['description'])。 ''';
echo'venue ='。 parseToXML($ row ['venue'])。 ''';
echo'activity ='。 parseToXML($ row ['activity'])。 ''';
}
?>

当我选择复选框,所有的echo语句都可以正常工作,并在屏幕上显示GFOption =1的所有记录,但是当我查看页面源时,所有记录都会在那里被回显然而,这对于else语句中的任何内容都是特定的如果复选框被选中或取消选中,则执行else语句中的任何内容,并且如果有输出,则会打印到页面源代码中。



另一个有趣的事情是,如果我硬编码我的if条件语句为

  if(true)

  if (false)

这些陈述完全按照他们应该的方式工作,并且没有奇怪的页面源打印发生。 / p>

如果我这样做的话

$ $ p $ $ $ $ c $ var_dump(isset($ _ POST ['GFCheckbox '])

我得到正确的布尔值值取决于GFCheckbox是否被选中。



正如你所看到的,我正在用这个圈子运行,非常感谢任何帮助。

解决方案

使用$ _GET或$ _SESSION变量



问题归结为Google Chrome。我遇到的问题是软件中公认的错误: http:// code.google.com/p/chromium/issues/detail?id=523 ,我相信他们正在修复Google Chrome的未来版本。



我将浏览器切换到Firefox和资源管理器,他们正确地进行了翻译,甚至是我之后的XML输出。

p>

这个解决方案的优点是出于Roe指出的潜在浏览器问题。

根据其他职位使用$ _GET而不是$ _POST应该可以正常工作。 $ _SESSION变量也应该这样做。


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.

解决方案

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.

这篇关于$ _POST问题,如果isset()和mysql_fetch_assoc():echo返回正确的值,页面源始终返回其他值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆