dp152153 2014-05-15 22:32
浏览 70
已采纳

检查ID是否存在的声明php [关闭]

This code receives the staffID from a input text box and then displays details or purchases of that staffID. I want to be able to make a possible statement(IF statement most likely) that checks that the staffID exists and if it doesn't "invalid staff Id" is echo'd. Can anyone help please?

<?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条回答 默认 最新

  • dongwei5794 2014-05-15 22:48
    关注

    I think you just want to use the mysql_num_rows function to get the number of rows in a result.

    <?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_* functions is deprecated as of PHP 5.5.0, and is not recommended for writing new code as it will be removed in the future. Instead, either the mysqli or PDO_MySQL extension should be used.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程