我遇到了一个问题,我的治疗记录包含6行数据,我即将到来的约会行只包含1行数据,但现在我的约会行也被复制成6行作为我的第一个表。
这里是mysql代码:
if(empty($_GET['name'])) {
echo 'Please don\'t try to hack!<br /><a href="./" title="Retry">Click here to <b>retry</b> again</a>';
exit;
}
require_once 'dbconnect.php';
$p_id = $_GET['name'];
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT *
FROM appointment a
LEFT JOIN customer c
ON a.customer_id = c.cust_id
AND a.deleted = '0'
LEFT JOIN services s
ON c.cust_id = s.customer_id
AND s.deleted = '0'
WHERE c.slug LIKE :id";
$q = $conn->prepare($sql);
$q->execute(array('id' => $p_id));
$q->setFetchMode(PDO::FETCH_ASSOC);
$cs = $q->fetchAll();
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
这是我的html视图代码:
<div id="container">
<h1>Customer Record</h1>
Name: <?php echo htmlspecialchars($cs[0]['fname'].' '.$cs[0]['lname']); ?> <br />
Gender: <?php echo htmlspecialchars($cs[0]['gender']); ?> <br />
Mobile Number: <?php echo htmlspecialchars($cs[0]['number']); ?> <br /><br />
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Treatment</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($cs as $r):?>
<tr>
<td><?php if($r['treatment'] === NULL) { echo "No Record Found"; } else { echo htmlspecialchars($r['treatment']); } ?></td>
<td><?php if($r['date'] === NULL) { echo "No Record Found"; } else { echo htmlspecialchars($r['date']); } ?></td>
<td><?php if($r['service_id'] === NULL) { echo "No Record Found"; } else { ?><a href="editrecord.php?name=<?php echo htmlspecialchars($r['slug']) ?>&id=<?php echo htmlspecialchars($r['service_id']) ?>">Edit Record</a> or <a href="deleterecord.php?name=<?php echo htmlspecialchars($r['slug']) ?>&id=<?php echo htmlspecialchars($r['service_id']) ?>">Delete Record</a><?php } ?></td>
</tr>
<?php endforeach; ?>
</tbody>
<thead>
<tr>
<th>Upcoming Appointment</th>
<th>Appointment Type</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach($cs as $r):?>
<tr>
<td><?php if($r['appt_date'] === NULL) { echo "No Record Found"; } else { echo htmlspecialchars($r['appt_date']); } ?></td>
<td><?php if($r['appointment_type'] === NULL) { echo "No Record Found"; } else { echo htmlspecialchars($r['appointment_type']); } ?></td>
<td><?php if($r['service_id'] === NULL) { echo "No Record Found"; } else { ?><a href="editrecord.php?name=<?php echo htmlspecialchars($r['slug']) ?>&id=<?php echo htmlspecialchars($r['service_id']) ?>">Edit Record</a> or <a href="deleterecord.php?name=<?php echo htmlspecialchars($r['slug']) ?>&id=<?php echo htmlspecialchars($r['service_id']) ?>">Delete Record</a><?php } ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<a href="./">Seach Again</a><br /><br />
<a href="addrecord.php?name=<?php echo htmlspecialchars($r['slug']) ?>">Add Record</a>
</div>
我不知道哪里出错了,但我怀疑我的sql代码部分出错了。
这是SQLFiddle for LEFT JOIN Appointment Table
以下是SQLFiddle for LEFT JOIN Appointment Table and LEFT JOIN Services Table
我需要这三个表数据来显示所有信息,如果我只使用来自约会表的左连接它显示6行没有重复错误,但如果我使用左连接约会表和左连接服务表,我的治疗表结果显示6行是正确的,但我的Upcoming Table也有6行相同的结果,而在数据库中只有1行数据。
请告知我哪里出错了。