会话未将POSTed数据传递给第二个脚本 - 请告知

时间:2015-07-20 22:47:54

标签: php mysql session-variables

试过问这个问题,它被标记为重复并附有建议。我使用的建议似乎仍然没有达到预期的效果。有人真的可以阅读以下内容并告知我哪里出错了吗?

我正在尝试使用会话将用户选择的数据从此脚本(allotment.php)传递到后续脚本(allotmentreport.php),其中它在查询限定符中使用(例如... WHERE tablecolumndata = session variable ...)。我没有在选择该选项并单击SUBMIT时从allotment.php收到错误但是数据无法传递给allotmentreport.php并返回未定义变量的错误。

这是否正确?有没有我错过的东西? 的 $ tourselect =(isset($ _ POST [ '提交']));

更新最终和更正的代码显示在下方,供未来的用户寻求实用示例和易于阅读的解决方案:

    <?php 
    session_start();
    $host="localhost";
    $username="HIDDEN";
    $password="HIDDEN";
    $dbname="bookings2015";
    $con = mysql_connect($host, $username, $password, $dbname);
    if (!$con)
    {
    die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
    }
mysql_select_db($dbname, $con);
?>
<!Doctype html>
<html>
<?php include 'C:\xampp\htdocs\phpproject1\head.php'; 
include 'config\menu.php';
?> 
<div id="dataentry">
<div id="submit">
<?php
echo "Which Tour to check availability?&nbsp;&nbsp;&nbsp;";?>
<br />
<br />
</br>
</br>
<form method="post" action="allotment_report.php">
<select name='TourCode'>
<?php
$tourselection = "SELECT DISTINCT TourCode FROM toursanddates ORDER BY TourCode";
$result = mysql_query($tourselection);
while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['TourCode'] . "'>" . $row['TourCode'] . "</option>";
}
?>
</select>
<input type="submit" name="tourselected" value="submit">
</form>
<?php
?>
</div>
</div>
<div id="demographicborder">
<?php 
include 'footer.php';?>     
        </div>
</div>
    </body>
</html>
</form>
</form>
</div>
</div>
</div>
</body>
</html>

,这是allotment_report.php代码

        <?php 
        session_start();
        $host="localhost";
        $username="STILLHIDDEN";
        $password="STILLHIDDEN";
        $dbname="bookings2015";
        $con = mysql_connect($host, $username, $password, $dbname);
        if (!$con)
        {
        die ('damn thing wont connect to the MYSQL server: Maybe it is retarded '. mysql_error());
        }
    mysql_select_db($dbname, $con);
    include 'C:\xampp\htdocs\phpproject1\head.php'; 
    include 'config\menu.php';
    ?> 
    <br />
    <br />
    <?php

//Table Header:
echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong><u>Tour Availability</u>";
    echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
    <tr>
        <td align=:"left"><b>Tour:</b></td>
        <td align=:"left"><b>Start Date:</b></td>
        <td align=:"left"><b>Seats Avail:</b></td>
        <td align=:"left"><b>Rooms Avail:</b></td>
    </tr>
    ';
    if(isset($_POST['TourCode'])){
            $tour=$_POST['TourCode'];
        }
    $status="ok";
    $ar="SELECT TourCode, DATE_FORMAT (TourStart, '%m%d%y') AS TourStart, SeatsAvail, RoomsAvail FROM toursanddates WHERE TourCode='$tour' AND Status='$status' ORDER BY TourCode, TourStart ASC"; 
    $result=mysql_query($ar);
    $num_results = mysql_num_rows($result);
    while($row = mysql_fetch_assoc($result)){
    //Display the allotments fetched in above query
    echo '<tr>
            <td align=:"left">' . $row['TourCode'] . '</td>
            <td align=:"left">' . $row['TourStart'] . '</td>
            <td align=:"left">' . $row['SeatsAvail'] . '</td>
            <td align=:"left">' . $row['RoomsAvail'] . '</td> 
            </tr>
            ';
    }
    echo '</table>';
    //echo "</strong>Tour:&nbsp;&nbsp;".($row['TourCode']);
    //echo "</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Start Date: ".($row['TourStart']);
        ?>
    <br />
        <?php
    echo "<br />";
    echo "</p>";
    ?>
    </br>
    </br>
    </div>
    </form> 
    </div>
    <div id="demographicborder">
    <?php include 'footer.php';
    ?>      
            </div>
    </div>
        </body>
    </html>
    </form>
    </form>
    </div>
    </div>
    </div>
    </body>
    </div>
    </body>

1 个答案:

答案 0 :(得分:1)

<?php
    //contained within allotment.php
    $tourselect=(isset($_POST['tourselected']));
    $_SESSION['tourselected']=$tourselect;
?>

看起来你期望在allotment.php上设置$_SESSION['tourselected'],当用户加载第一次打开页面时。然而,这种情况并非如此。 $_POST数据附加了HTTP请求。当您第一次加载alloment.php时,浏览器不会向其发送任何$_POST数据。这可以解释为什么当你到达第二个脚本时$_SESSION['tourselected']未被设置。

也就是说,如果您的唯一目标是将数据从alloment.php中构建的表单发送到alloment_report.php ,那么您根本不应该使用会话。所有这一切都可以仅使用$_POST完成。

考虑以下代码:

 <!--alloment.php-->
    <form method="post" action="alloment_report.php">
        <select name='TourCode'>
            <?php 
            //Assume that $options contains stuff pulled from your database.
            foreach($options as $option) {
                echo "<option value='" . $option . "'>" . $option . "</option>";
            }
            ?>
        </select>
        <input type="submit" name="tourselected" value="submit">
    </form>

当用户填写表单并单击“提交”时,alloment_report.php(由action="alloment_report"指定)获取表单上发送的数据$_POST(由method="post"指定)。< / p>

<!--alloment_report.php-->
    <?php
    if(isset($_POST['tourcode'])){
        echo "yay! the tour has been selected!";
    }
    ?>