声明检查ID是否存在php

时间:2014-05-15 22:32:51

标签: php mysql forms

此代码从输入文本框接收staffID,然后显示该staffID的详细信息或购买。我希望能够做出一个可能的声明(最有可能的IF声明)来检查staffID是否存在以及它是否“无效的员工ID”是echo。有人可以帮忙吗?

<?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
$conn = mysql_connect("localhost", "twa291", "twa291up");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );  ?>

<?php
$staffid= $_GET["staffID"];


?>

<?php

$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase, 
staff 
WHERE staff.staffID='$staffid'"; 


$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>

1 个答案:

答案 0 :(得分:0)

我认为您只想使用mysql_num_rows函数来获取结果中的行数。

<?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
$conn = mysql_connect("localhost", "twa291", "twa291up");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() ); 

$staffid= $_GET["staffID"];

$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase, 
staff 
WHERE staffID = $staffid"; 

$rs = mysql_query($sql, $conn) or die ('Problem with query' . mysql_error());

$num_rows = mysql_num_rows($rs);

if($num_rows == 0){
    echo "invalid staff Id";
    die;
} else{

?>

<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>

mysql _ * 函数自PHP 5.5.0起不推荐使用,不建议用于编写新代码,因为将来会删除它们。相反,应使用mysqliPDO_MySQL扩展名。