简体   繁体   English

PHP:表单POST之后发生致命错误

[英]PHP: Fatal error after a form POST

The following code throws an error in PHP 5.2.9 after submission, but not on the original load. 提交后,以下代码在PHP 5.2.9中引发错误,但原始加载时未引发错误。 Error: Fatal error: Only variables can be passed by reference in /home/golfcom/public_html/test.php on line 12 错误:致命错误:只能在第12行的/home/golfcom/public_html/test.php中通过引用传递变量

File: 文件:

<?php
include('connection.php');
$result = $dbc->query("SELECT subdivision FROM Residential");
$search['subdivision'] = array();

while($i = $result->fetch_array()){
    echo $i['subdivision'];
    array_push($search['subdivision'], $y = $i['subdivision']);
}
?>
<form action="test.php" method="post">
<input type='submit' value='search' class='submit' name='search' /></form>

Just a guess, but maybe it has something to do with the assignment operation inside the function: 只是一个猜测,但也许与该函数内部的赋值操作有关:

array_push($search['subdivision'], $y = $i['subdivision']);

Try this instead: 尝试以下方法:

$y = $i['subdivision'];
array_push($search['subdivision'], $y);

Figured it out. 弄清楚了。 My host had register_globals on. 我的主机打开了register_globals。 Turning if off fixed everything. 如果关闭,则修复所有问题。

Ignore this question, I'm getting inconsistent results. 忽略这个问题,我得到的结果不一致。 I think my host is wonky. 我认为我的主人很古怪。

You have no error control. 您没有错误控制。 You need to check the following: 您需要检查以下内容:

  • your database connection is OK and connected 您的数据库连接正常并且已连接
  • the query did not error and returned a valid result (even if there are no rows) 查询没有错误并返回有效结果(即使没有行)
  • dont try to print a row if there was none returned by the query 如果查询未返回任何行,请不要尝试打印行

In your loop, $y will be the value of the LAST row and is set every loop. 在循环中,$ y将是LAST行的值,并在每个循环中设置。 While its not incorrect, you can change your code to something like: 虽然它不正确,但是您可以将代码更改为:

$t = $result->numRows();
while ($row = $result->fetch_array()) {
    $search['subdivision'][] = $row['subdivision'];
}
$lastResult = $search['subdivision'][$t - 1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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