duanba5777 2013-05-15 02:53
浏览 33
已采纳

在PHP中为空数据库查询生成错误消息

The following code's purpose is to get an input (from a separate HTML file) for the customerID in a database from the user and then display the order number, order date and shipped status for that customerID. The code works fine and I am able to do this, however I also want to create an error message if a customerID that does not exist in the database is entered, instead of just an empty table. I am new to PHP and any help on how to do this is appreciated. (Please note, it has to be in either PHP or mysql)

<?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 8</title>
</head>
<body>
<?php
$conn = mysql_connect("localhost", "<username>", "<password>");
mysql_select_db("warehouse<##>", $conn) 
or die ('Database not found ' . mysql_error() );
$input = $_GET["custID"];
$sql = "select orderNumber, orderDate, shipped from orders where customerID = $input 
order by orderDate"; 
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<?php 
if (orderNumber != "") { ?> 
<p>the following information was received from the user:</p>
<p><strong>customerID = </strong> <?php echo "$input"; ?><br/><br/>

<table border="1" summary="Order Details">
<tr>
<th>Order Number</th>
<th>Order Date</th>
<th>Shipped</th>
</tr>
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["orderNumber"]?></td>
<td><?php echo $row["orderDate"]?></td>
<td><?php echo $row["shipped"]?></td>

</tr>
<?php }}
else {
$txt ="The CustomerID you entered was either invalid or does not exist"; 
echo $txt;?>
<?php }
mysql_close($conn); ?>
</table>
</body></html>
  • 写回答

2条回答 默认 最新

  • dongpao5127 2013-05-15 04:05
    关注
    <?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 8</title>
    </head>
    <body>
    <?php
    $conn = mysql_connect("localhost", "<username>", "<password>");
    mysql_select_db("warehouse<##>", $conn) 
    or die ('Database not found ' . mysql_error() );
    $input = $_GET["custID"];
    $sql = "select orderNumber, orderDate, shipped from orders where customerID = $input 
    order by orderDate"; 
    $rs = mysql_query($sql, $conn)
    or die ('Problem with query' . mysql_error());
    //validate result set here
    if(mysql_num_rows($rs)>0)
    {
    ?>
    <?php 
    if (orderNumber != "") { ?> 
    <p>the following information was received from the user:</p>
    <p><strong>customerID = </strong> <?php echo "$input"; ?><br/><br/>
    
    <table border="1" summary="Order Details">
    <tr>
    <th>Order Number</th>
    <th>Order Date</th>
    <th>Shipped</th>
    </tr>
    <?php
    while ($row = mysql_fetch_array($rs)) { ?>
    <tr>
    <td><?php echo $row["orderNumber"]?></td>
    <td><?php echo $row["orderDate"]?></td>
    <td><?php echo $row["shipped"]?></td>
    
    </tr>
    <?php }}
    else {
    $txt ="The CustomerID you entered was either invalid or does not exist"; 
    echo $txt;?>
    <?php }
    
    }//endif
    else{
    
    //you error message here
    }
    
    mysql_close($conn); ?>
    </table>
    </body></html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FOC simulink
  • ¥15 咨询一下有关于王者荣耀赢藏战绩
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?