-1

我对下面的代码有什么问题。我试图让它回显到另一个页面,但它似乎没有拾取数据库上的数据。

<?php
{
    // connect to server
    mysql_connect("localhost", "u", "") or die(mysql_error());
    //select database
    mysql_select_db("") or die(mysql_error());

    //Create a query that selects all data from the PATIENT table where the username and password match
    $query = "SELECT`Appointment_id`, `Doctor_id`, `Patient_id`, `Appointment_time`, `Appointment_date` FROM `Appointment`";

    //executes query on the database
    $result = mysql_query ($query) or die ("didn't query");
    //this selects the results as rows

    $num = mysql_num_rows ($result);
    //if there is only 1 result returned than the data is ok 
    if ($num == 1)
    {
        $row=mysql_fetch_array($result);
        $_SESSION['Appointment_id'] = $row['Appointment_id'];
        $_SESSION['Doctor_id'] = $row['Doctor_id'];

    }
}
?>

约会.php

<!DOCTYPE html>
<?php
session_start();
?>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="my.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
        </script>
        <script src="my.js">
        </script>
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
        <script>
            try {

    $(function() {

    });

  } catch (error) {
    console.error("Your javascript has an error: " + error);
  }
        </script>
    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d"  data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                 Home  
                </a>

            </div>
            <div data-role="content">

<?php
{
    // connect to server
    mysql_connect("localhost", "", "") or die(mysql_error());
    //select database
    mysql_select_db("") or die(mysql_error());


    $query = "SELECT `Appointment_id`, `Doctor_id`, `Patient_id`, `Appointment_time`, `Appointment_date` FROM `Appointment`";

    //executes query on the database
    $result = mysql_query ($query) or die ("didn't query");
    //this selects the results as rows

    $num = mysql_num_rows ($result);
    //if there is only 1 result returned than the data is ok 
    if ($num == 1)
    {
        $row=mysql_fetch_array($result);
        $_SESSION['Appointment_id'] = $row['Appointment_id'];
        $_SESSION['Doctor_id'] = $row['Doctor_id'];

    }
}
?>
            </div>
        </div>
    </body>
</html>

回显到此页面

<!DOCTYPE html>
<?php
session_start();
?>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <title>
        </title>
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="my.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
        </script>
        <script src="my.js">
        </script>
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
        <script>
            try {

    $(function() {

    });

  } catch (error) {
    console.error("Your javascript has an error: " + error);
  }
        </script>
     </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <a data-role="button" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                 Home  
                </a>
                <h3>
                    Details
                </h3>
            </div>
            <div data-role="content">
              <form name="form1" method="post" action="login.php">
            <strong>Your Details</strong>
            <br />
            <br />
            Appointment: <?php echo $_SESSION['Appointment_id'];?>
            <br />
            <br />
            Doctor: <?php echo $_SESSION['Doctor_id'];?>
            <br />
            <br />



             </div>
        </div>
    </body>
</html>

第二部分是数据库获取信息的位置(约会),第三部分显示我需要它显示的位置

<!DOCTYPE html>
<?php
session_start();
?>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <meta http-equiv="refresh" content="2;url=details1.php">
        <title>
        </title>
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="my.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
        </script>
        <script src="my.js">
        </script>
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
        <script>
            try {

    $(function() {

    });

  } catch (error) {
    console.error("Your javascript has an error: " + error);
  }
        </script>
    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                 Home  
                </a>
                <h3>
                    Login Process
                </h3>
            </div>
            <div data-role="content">

登录.php

   <!DOCTYPE html>
<?php
session_start();
?>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta name="apple-mobile-web-app-capable" content="yes" />
        <meta name="apple-mobile-web-app-status-bar-style" content="black" />
        <meta http-equiv="refresh" content="2;url=details1.php">
        <title>
        </title>
        <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <link rel="stylesheet" href="my.css" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">
        </script>
        <script src="https://ajax.aspnetcdn.com/ajax/jquery.mobile/1.2.0/jquery.mobile-1.2.0.min.js">
        </script>
        <script src="my.js">
        </script>
        <!-- User-generated css -->
        <style>
        </style>
        <!-- User-generated js -->
        <script>
            try {

    $(function() {

    });

  } catch (error) {
    console.error("Your javascript has an error: " + error);
  }
        </script>
    </head>
    <body>
        <!-- Home -->
        <div data-role="page" id="page1">
            <div data-theme="a" data-role="header">
            <a data-role="button" data-theme="d" href="login.html" data-icon="arrow-l" data-iconpos="left" class="ui-btn-left">
                    Back
                </a>
                <a data-role="button" href="index.html" data-icon="home" data-iconpos="right" data-theme="d"class="ui-btn-right">
                 Home  
                </a>
                <h3>
                    Login Process
                </h3>
            </div>
            <div data-role="content">

<?php
// takes the variables from action script and assigns them php variables names
$user = $_POST ['username'];
$pass = $_POST ['password'];

// if there is a user name and password
if ($user && $pass)
{
    // connect to server
    mysql_connect("localhost", "", "") or die(mysql_error());
    //select database
    mysql_select_db("") or die(mysql_error());

    //Create a query that selects all data from the PATIENT table where the username and password match
    $query = "SELECT * FROM Patient WHERE Username = '$user' AND Password = '$pass'";

    //executes query on the database
    $result = mysql_query ($query) or die ("didn't query");
    //this selects the results as rows

    $num = mysql_num_rows ($result);
    //if there is only 1 result returned than the data is ok 
    if ($num == 1)
    {
        //sends back a data of "Success"
        echo "Successful Login";
        $row=mysql_fetch_array($result);
        $_SESSION['Title'] = $row['Title'];
        $_SESSION['First_name'] = $row['First_name'];
        $_SESSION['Last_name'] = $row['Last_name'];
        $_SESSION['Address'] = $row['Address'];
        $_SESSION['Line_2'] = $row['Line_2'];
        $_SESSION['Line_3'] = $row['Line_3'];
        $_SESSION['Postcode'] = $row['Postcode'];
        $_SESSION['Home'] = $row['Home'];
        $_SESSION['Mobile'] = $row['Mobile'];



    }
    else
    {
        //sends back a message of "failed"
        echo "Unsuccessful Login";
    }
}

?>
            </div>
        </div>
    </body>
</html>
4

5 回答 5

0

session_start()在两个页面上使用,如果您不启动将无法使用

编辑

创建一个测试页面以查看您的会话是否正常工作?

如果是,则在其他页面上进行测试

还可以尝试在您设置会话值的 if 条件中添加一个简单的回显。如果该回显触发,则意味着您的代码是正确的,并且 SESSION 有问题。如果 echo 没有触发,则表示您的代码不正确并且未设置会话

于 2013-03-29T04:27:37.423 回答
0

请在顶部添加

session_start();
于 2013-03-29T04:27:54.630 回答
0

编辑:

好的,所以我相信逻辑错误在于

$query = "SELECT `Appointment_id`, `Doctor_id`, `Patient_id`, `Appointment_time`, `Appointment_date` FROM `Appointment`";

$result = mysql_query ($query) or die ("didn't query");
$num = mysql_num_rows ($result);
if ($num == 1)
{

}

由于您的查询没有WHERE子句,因此它返回所有记录,并且您仅在有 1 条记录时设置 SESSION 值。您还应该else有条件查看实际发生的情况。

Create a query that selects all data from the PATIENT table where the username and password match

但是您的查询不会这样做,它会获取所有PATIENTs 的数据

例如,如果您的用户名和密码来自POSTed 表单,那么您可以这样做

$username=mysql_real_escape_string($_POST["username"]);
$password=mysql_real_escape_string($_POST["password"]);

WHERE然后你可以在一个子句中使用这两个值。由于除了阅读您的查询之外,我不知道您的表的结构,因此我无法准确地写出来。但只是给你一个想法,一个地方可能像

$query="SELECT id from YourTable where $username='$username' and password='$password'"

非常重要的是要注意,因为您是初学者mysql_*不应再使用函数。如果你要开始,不要从已经消失的东西开始。阅读mysqli_*PDO

编辑2:

好的,那就这样做吧

  1. 成功登录后,您将患者的详细信息存储在 SESSION 中,也可以存储Patient_id类似$_SESSION['Patient_id'] = $row['Patient_id'];// 或者可能只是id. 检查你的桌子
  2. 在 Appointment.php 上将您的查询更改为

    $pid=intval($_SESSION["Patient_id"]); $query = "SELECT Appointment_id, Doctor_id, Patient_id, Appointment_time, Appointment_dateFROM Appointmentwhere Patient_id=$pid";

  3. if($num==1) { }在您的块下方添加一个 else 条件,例如

PHP

else{
echo "Somehow we didn't get 1 record";
die();
}
于 2013-03-29T04:28:14.933 回答
0

因为您正在使用$_SESSION.. 您需要在 php 开始标记之后立即使用 session_start()

<?php
  session_start();

不仅在此页面上.. session_start() 应该在您希望使用的每个页面上都有session_start().. 否则$_SESSION无法访问..

于 2013-03-29T04:29:40.073 回答
0

如果您只是想获得第一行

$query = "SELECT `Appointment_id`, `Doctor_id`, `Patient_id`, `Appointment_time`, `Appointment_date` FROM `Appointment` WHERE rownum = 1 ; ";
于 2013-03-29T04:56:06.637 回答