PHP回显到另一个页面

时间:2013-03-29 04:26:01

标签: php

下面这段代码我错了。我试图让它回显到另一个页面,但它似乎没有拿起数据库上的数据。

<?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'];

    }
}
?>

Appointment.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">

的login.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>

5 个答案:

答案 0 :(得分:0)

请在两个页面上使用session_start(),如果您没有启动,则无法使用

修改

创建一个测试页面以查看您的会话是否有效?

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

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

答案 1 :(得分:0)

请在顶部添加

session_start();

答案 2 :(得分: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

但是您的查询不会这样做,它会获取所有PATIENT s

的数据

例如,如果您的用户名和密码来自POST格式,那么您可以

$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_idDoctor_idPatient_idAppointment_timeAppointment_date FROM Appointment,其中Patient_id = $ pid”;

  3. if($num==1) { }块下方添加其他条件,例如

  4. PHP

    else{
    echo "Somehow we didn't get 1 record";
    die();
    }
    

答案 3 :(得分:0)

因为你正在使用$_SESSION ..你需要在php打开标签后立即使用session_start()

<?php
  session_start();

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

答案 4 :(得分:0)

如果你只是试图获得第一行

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