【问题标题】:Getting undefined index mysql session获取未定义的索引mysql会话
【发布时间】:2013-03-16 13:09:48
【问题描述】:

我收到以下代码错误。我有两个 php 文件。一种是从文本字段中获取输入的表单,我希望将其传递给下一个文件。这是我目前所拥有的:

<?php
if(isset($_GET['submit']))
{
    $_SESSION['search'] = $_GET['patient_id'];
    header('Location:./reception_view_results.php');
}
else{
?>
<!DOCTYPE html>
<html>
<head>
    <title>TEST</title>
</head>
<body>
    <fieldset>
        <legend>Search Appointment</legend>
        <form method="GET">
            <label>Patient ID</label>
            <input type="text" name="patient_id"/>
            <input type="submit" name="submit" value="Search"/>
        </form>
    </fieldset>
</body>
</html>
<?php 
} 
?>

以及该值将被传递到的后续页面:

<!DOCTYPE html>
<html>
<head>
    <title>View Results</title> 
</head>
<body>
    <?php 
    session_start();

    include("connect.php");

    $search = $_SESSION['search'];

    if ($result = $db->query("SELECT nextapptdate, nextappttime, doc_id pt_id, pt_fname, pt_lname, ph_no 
    FROM patient 
    WHERE pt_id = $search
    ORDER BY pt_id;"))
    {
        // display records if there are records to display
        if ($result->num_rows > 0)
        {?>
            <fieldset>
            <legend><strong>Search Results</strong></legend>
            <table>
                <thead>
                    <tr>
                        <th>Patient ID
                        <th>First Name
                        <th>Last Name
                        <th>Date of Birth
                        <th>Doc ID
                        <th>Phone No.
                        <th>Next. Appt.
                        <th>Time
                </thead>
                <tbody>
                    <?php
                    while ($row = $result->fetch_object())
                    {   
                        // set up a row for each record
                        echo "<tr>";
                        echo "<td>" . $row->pt_id;
                        echo "<td>" . $row->pt_fname;
                        echo "<td>" . $row->pt_lname;
                        echo "<td>" . $row->ptdob;
                        echo "<td>" . $row->doc_id;
                        echo "<td>" . $row->ph_no;
                        echo "<td>" . $row->nextapptdate;
                        echo "<td>" . $row->nextappttime;
                    }?>
                    </tbody>
            </table>
            </fieldset>
        <?php
        }
    else{echo "No results to display!";}
    }
    ?>  
</body>
</html>

我已经尝试了几件事:
1) 从 POST 更改为 GET。
2) 完全从表单中移除 action 属性。
3) 尝试直接使用会话变量。

我不断收到此行的错误:

if ($result = $db->query("SELECT nextapptdate, nextappttime, doc_id pt_id, pt_fname, pt_lname, ph_no 
            FROM patient 
            WHERE pt_id = $search
            ORDER BY pt_id;"))
            {

那是因为它不懂$search。

感谢大家的帮助。

【问题讨论】:

  • 我会尝试在将值分配给会话之后和重定向之前添加session_write_close
  • JazakAllah khayr 的建议。

标签: php mysql forms session indexing


【解决方案1】:

您在 doc_id 后缺少一个逗号。它应该像这样工作(包括一些格式)

SELECT
  nextapptdate,
  nextappttime,
  doc_id,
  pt_id,
  pt_fname,
  pt_lname,
  ph_no 
FROM
  patient 
WHERE
  pt_id = $search
ORDER BY
  pt_id

并查看您的第一个文件:全局数组 $_SESSION 不存在,直到您调用第一个文件中缺少的 session_start 并且会产生您在主题中遇到的错误。

【讨论】:

  • JazakAllah khayr!我已经厌倦了看这个大约 2 天了。度过了我的夜晚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-18
  • 1970-01-01
  • 2015-03-06
  • 2017-02-02
  • 2016-06-07
相关资源
最近更新 更多