数据不在php中显示

时间:2013-04-01 05:50:52

标签: php mysql join phpmyadmin

我有这个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" 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>
                    Book appointment
                </h3>
           </div>

           <div data-role="content">
                <h3>
                    Select date/time:
                </h3>
                <br />
<?php
{
    mysql_connect("localhost" , "" , "") or die (mysql_error());
    mysql_select_db("") or die(mysql_error());


    $pid=intval($_SESSION["Patient_id"]); $query = "SELECT t1.*, t2.Doctor_name, t2.Doctor_room FROM Appointment AS t1 INNER JOIN Doctor AS t2 ON t1.Doctor_id=t2.Doctor_id";

    //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_date'] = $row['Appointment_date'];
        $_SESSION['Appointment_time'] = $row['Appointment_time'];
        $_SESSION['Doctor_name'] = $row['Doctor_name'];
        $_SESSION['Doctor_room'] = $row['Doctor_room'];
    }
}
?>  

        <strong>Dates available</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Select date--</option>
        <option value="3"><?php echo $_SESSION['Appointment_date'];?></option>
        </select>

        <br />
        <br />

        <strong>Times available</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Select time--</option>
        <option value="3"><?php echo $_SESSION['Appointment_time'];?></option>>
        </select>

        <br />
        <br />

            <strong>Doctor Name</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Name--</option>
        <option value="2"><?php echo $_SESSION['Doctor_name'];?></option>>
        </select>

        <br />
        <br />

            <strong>Doctor Room</strong>            
        <select id="Availability" name="Availability">                      
        <option value="0">--Room--</option>
        <option value="2"><?php echo $_SESSION['Doctor_room'];?></option>>
        </select>

        <br />
        <br />

                <label for="textarea1">
                Message GP
                </label>
                <textarea name="" id="textarea1" placeholder="">
                </textarea>

                <br />
                <br />

        <a data-role="button" data-theme="a" href="booked.php">
                    Book Appointment
                </a>  

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

我进入了phpmyadmin并插入了另一个约会,因此用户可以选择,但此约会不会显示,只显示一个。有什么想法吗?

谢谢

1 个答案:

答案 0 :(得分:2)

这实际上是一个有趣的错误

if ($num == 1) {}
    {
        $row=mysql_fetch_array($result);
        $_SESSION['Appointment_date'] = $row['Appointment_date'];
        $_SESSION['Appointment_time'] = $row['Appointment_time'];
        $_SESSION['Doctor_name'] = $row['Doctor_name'];
        $_SESSION['Doctor_room'] = $row['Doctor_room'];
    }

您将其设置为仅在$num==1时显示数据,但由于这些额外的{},即使记录多于一个,也会显示数据。因为该显示不在if块内,if对您的显示没有任何影响。但是你没有看到更多记录的原因是因为你只提取一次而不是循环

必须像

while($row=mysql_fetch_assoc($result))
{
        $_SESSION['Appointment_date'] = $row['Appointment_date'];
        $_SESSION['Appointment_time'] = $row['Appointment_time'];
        $_SESSION['Doctor_name'] = $row['Doctor_name'];
        $_SESSION['Doctor_room'] = $row['Doctor_room'];
}