【问题标题】:PHP form script works on desktop, but not on serverPHP 表单脚本适用于桌面,但不适用于服务器
【发布时间】:2010-09-29 22:45:29
【问题描述】:

我被难住了。我正在用 PHP 做一个基本的表单,它在库存的 Ubuntu 10.04 桌面安装上完美运行,但在使用 Ubuntu 9.10 的网络服务器上运行时失败,据我所知,配置没有明显差异。

一个会话变量($_SESSION['id'])在上一页被设置为1,然后脚本包括:
(1) session_start();
(2) SQL查询根据id的session值从db表中拉出一条记录(select * from products where id=$_SESSION['id'])
(3) 打印表单本身的功能,必要时会显示错误消息。该表单只是几个名为“意见”的单选按钮,如果没有按下任何按钮,则会显示错误消息。
(4) 代码:

if (!isset($_POST['submit']))// if the Submit button has not been pressed ...
{
   showpage($error=false); // ... show the form
}
else
{
    if (!isset($_POST['opinion'])) // if Submit was pressed, but no radio button ...
    {
        showpage($error=true); // ... reprint the form with the error message 
    }
    else // if both radio button and Submit were pressed
    {
        $_SESSION['id']=$_SESSION['id']+1; // increment the session variable
        $_SESSION['opinion']=$_POST['opinion']; // convert the radio button value into a session variable
        header("Location: myratings.php"); // present a new page which will be based on the incremented id
    }
}

在桌面上,按“提交”按预期会带您浏览表中的所有项目,每次都会显示一个新表单。但是在服务器上,按下提交会产生一个空白页面,并且只有当您按下返回按钮时才会出现预期的新表单。

在桌面上,当您按下单选按钮然后提交时,错误消息会消失,但在服务器上,如果已使用过一次,它似乎会一直出现。

这可能很简单,但我再也看不清楚了:-(。如果原因对任何人来说都是显而易见的,请让我摆脱痛苦。

更新:

好的 - 这是页面的代码 (opinions.php):

<?php 
session_start();
header("Content-type: text/html; charset=utf-8");
include("includes/header.php");

$id=$_SESSION['id'];

$sql="select * from products where id=$id";
$result=pg_query($db_handle,$sql) or die("Can't get the items");
while ($row=pg_fetch_object($result))
{
    $id=$row->id;
    $product=$row->product;
}

function showpage($product, $error=false)
{
    echo '<form action="opinions.php" method="POST">';
    echo '<table width="600" align="center">';
    echo '<tr><td colspan="3" class="ratings"><h3>How often would you use $product?</h3></td></tr>';
    echo '<tr>';
    echo '<td width="200" class="ratings">Never<input type="radio" name="opinion" value="1"></td>';
    echo '<td width="200" class="ratings">Sometimes<input type="radio" name="opinion" value="2"></td>';
    echo '<td width="200" class="ratings">Often<input type="radio" name="opinion" value="3"></td>';
    echo '</tr>';
    if ($error) {echo '<tr><td colspan="3" class="ratingserror">Please press a raddio button!</td></tr>';}
    echo '<tr><td colspan="3" class="ratings"><input type="submit" name="submit" value="Continue"></td></tr>';
    echo '</table>';
    echo '</form>';
}

if (!isset($_POST['submit'])) 
{
   showpage($product, $error=false);
}
else
{
    if (!isset($_POST['opinion']))
    {
        showpage($product, $error=true);
    }
    else
    {
       $_SESSION['id']=$_SESSION['id']+1;
       header("Location: opinions.php"); 
    }
}

include("includes/footer.php");
?>

header.php 和 footer.php 只包含页面家具。

【问题讨论】:

  • 对我来说没有什么明显的,但它可能有助于查看“显示页面”正在做什么,以及错误日志中可能包含的内容

标签: php


【解决方案1】:

根据您的描述 - 您不能在调用 session_start() 之前设置 $_SESSION['id']。也许发布页面的所有代码。还可以在某些地方抛出print_r($_SESSION) 进行额外测试。

【讨论】:

  • 添加在上面 - 关于$_SESSION['id'] 的观点是一个很好的,但为什么它在本地工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多