質問

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>
役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top