此编码要求用户输入他们的员工ID,然后搜索数据库并将他们的购买信息显示在表格中(orderID,shippingDate等)。我希望能够创建和IF语句(非常确定它和IF语句),确保输入的人员ID存在于数据库中,如果它继续下面的代码,否则它只显示没有表写工作人员id不存在。不包含html文件,因为它不需要。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 3</title>
</head>
<body>
<?php
$staffid = $_GET["staffID"];
if($staffid != $_GET["staffID"]){
echo "Id doesnt Exist";
}
else{
true;
}
?>
<?php
$conn = mysql_connect("localhost", "twa291", "twa291up");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );
$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase,
staff
WHERE
purchase.staffID = staff.staffID AND staff.staffID= '$staffid' ORDER BY purchase.orderDate ASC";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<table border="1" summary="Staff Orders">
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Staff Name</th>
</tr>
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["orderID"]?></td>
<td><?php echo $row["orderDate"]?></td>
<td><?php echo $row["shippingDate"]?></td>
<td><?php echo $row["staffName"]?></td>
</tr>
<?php }
mysql_close($conn); ?>
</table>
</body>
</html>
感谢您的帮助。
答案 0 :(得分:0)
在显示html之前先查询
<?php
$conn = mysql_connect("localhost", "twa291", "twa291up");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );
$sql = "SELECT orderID FROM purchase WHERE
purchase.staffID = " . $_GET["staffID"];;
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
if($rs != null ){
//show table
}
else{
echo "Id doesnt Exist";
}
?>
只需修复代码,逻辑就像这样