【问题标题】:Statement to check if ID exists php [closed]检查ID是否存在的语句php [关闭]
【发布时间】:2014-05-15 22:32:51
【问题描述】:

此代码从输入文本框中接收员工 ID,然后显示该员工 ID 的详细信息或购买情况。我希望能够做出一个可能的语句(最有可能的 IF 语句)来检查人员 ID 是否存在,如果它不存在“无效的人员 ID”,则会被回显。有人可以帮忙吗?

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

【问题讨论】:

  • mysql_ 系列函数已弃用。它被认为是一种安全风险。切换到 mysqli_ 系列函数或 PDO 类。
  • ...并使用准备好的语句
  • ...并以某种方式过滤输入,这样会留下很多漏洞

标签: php mysql forms


【解决方案1】:

我认为您只想使用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 扩展名。

【讨论】:

  • 真诚地感谢它的工作!
  • 请问,staffID 列我没选的时候,你怎么能统计行数?
  • @user3641114 mysql_num_rows 函数检查,如果不存在具有 StaffID 的记录,则返回零。
猜你喜欢
  • 1970-01-01
  • 2015-03-09
  • 2014-11-29
  • 2013-04-09
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
相关资源
最近更新 更多