PHP表单脚本适用于桌面,但不适用于服务器

时间:2010-09-29 22:45:30

标签: php

我很难过。我正在用PHP做一个基本的表单,它在股票Ubuntu 10.04桌面安装上运行得很好,但在web服务器上运行时失败了,据我所知,在使用Ubuntu 9.10时,配置没有明显的差异。

会话变量($ _SESSION ['id'])在上一页设置为1,然后脚本包括:
(1)session_start();
(2)一个SQL查询,用于根据id的会话值从db表中提取记录(select * from product = id = $ _ SESSION ['id'])
(3)打印表单本身的函数,必要时带有错误消息。表单只是一些名为“views”的单选按钮,如果没有按下它们,则会显示错误消息 (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
    }
}

在桌面上,按“提交”会按预期通过表格中的所有项目,每次都会显示一个新表单。但是在服务器上,按“提交”会生成一个空白页面,只有当您按“返回”按钮时才会显示预期的新表单。

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

这可能很简单,但我再也看不到了:-(。如果原因对任何人来说都是显而易见的,请把我从痛苦中解脱出来。

更新

好的 - 这是页面的代码(opinion.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只包含页面家具。

1 个答案:

答案 0 :(得分:0)

根据您的说明 - 在致电$_SESSION['id']之前,您无法设置session_start()。也许发布您页面的所有代码。同时在某些地方抛出print_r($_SESSION)以进行其他测试。