数据未在 php 中显示 [关闭]
Posted
技术标签:
【中文标题】数据未在 php 中显示 [关闭]【英文标题】:Data not displaying in php [closed] 【发布时间】:2013-03-22 05:23:45 【问题描述】:我有这个显示预订详情的 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 并插入了另一个约会,以便用户可以选择,但这个约会不显示,只显示一个。有什么想法吗?
谢谢
【问题讨论】:
检查了你的doctor
表..??确保从phpmyadmin
添加的record
属于doctor
表中的现有医生。
尝试删除 if ($num == 1)
if($num == 1)
之后我看到了什么?为什么会有一个空块?
您应该尝试通过打印我们的变量来解决问题,看看哪里出了问题。使用“var_dump($var);”调查并查看在哪里生成了什么。
【参考方案1】:
这其实是一个有趣的错误
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'];
【讨论】:
刚刚尝试了您建议的代码,没有出现错误,但仍然没有显示另一个日期和时间。 变量$num
返回的记录数是多少
和以前一样 if ($num == 1)
那么这意味着查询返回的记录不超过 1 条,也需要修复
试试 if($num > 0).....以上是关于数据未在 php 中显示 [关闭]的主要内容,如果未能解决你的问题,请参考以下文章
数据表服务器端处理未在页面上显示输出并且条件不起作用[关闭]