0

我目前对该程序有 2 个不同的部分,前半部分从网页获取用户输入,然后将其传输到 PHP 端,该端将访问 MySQL 并显示请求的信息。

示例:如果我输入 AX12 作为 ID,它将显示该 ID 的信息,该 ID 确实存在,但如果我输入 AX13(它不存在),它将显示空白信息,所以我想知道是否有人可以告诉我我如何一旦信息被传输到 PHP 端,就可以验证这一点。因此,如果它检测到您提交的信息不存在,只需显示一条消息“ID 不存在”或类似的内容。

如果您需要更多信息,这里是 PHP 端的代码。

<?php

$part_number = $_GET['txtInput'];
$part_description;
$units_on_hand;
$item_class;
$warehouse_number;
$unit_price;

$query;
$result_set;
$connection;
$record;

echo "<html>";
echo "<head>";
echo "<title>SQL Application</title>";
echo "<style type = 'text/css'>body{text-align: center; background-color: #CC3333; color: #660000; font-size: 30;}</style>";
echo "</head>";
echo "<body>";

echo "<center><h1>SQL Application</h1></center>";

echo "<br />";
echo "<br />";
echo "<br />";

$connection = @mysql_connect("localhost","m_stanicic","")
        or die ("\n\n PROBLEM CONNECTING TO DATABASE! \n" . mysql_error() . "\n\n");

mysql_select_db("m_stanicicdb");

$query = "select * from part where part_number = '" . $part_number . "'";

$result_set = mysql_query($query)
        or die ("\n\n PROBLEM WITH QUERY! . \n" . mysql_error() . "\n\n");

$record = mysql_fetch_assoc($result_set);

if($part_number == "")
{
        //
}
else
{
        $part_description = $record['part_description'];
        $units_on_hand = $record['units_on_hand'];
        $item_class = $record['item_class'];
        $warehouse_number = $record['warehouse_number'];
        $unit_price = $record['unit_price'];

        echo "<center>";
        echo "<table border='1' width=400 style ='table-layout:fixed' cellpadding='5' cellspacing='0'>";
        echo "<col width = 200>";
        echo "<col width = 200>";
        echo "<tr>";
        echo "<th colspan='2'>DETAILS OF THE PART YOU REQUESTED</th>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>part_description</td>";
        echo "<td>" . $part_description . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>units_on_hand</td>";
        echo "<td>" . $units_on_hand . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>item_class</td>";
        echo "<td>" . $item_class . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>warehouse_number</td>";
        echo "<td>" . $warehouse_number . "</td>";
        echo "</tr>";
        echo "<tr>";
        echo "<td>unit_price</td>";
        echo "<td>$" . $unit_price . "</td>";
        echo "</tr>";
        echo "</table>";
        echo "</center>";

        mysql_close($connection);
}

echo "<br />";
echo "<br />";
echo "<br />";

echo "<input type = 'button' value = 'RETURN' style = 'width: 75px; height: 75px;' onclick = \"javascript:window.location.href = 'jdpset1_4.html'\">";

echo "</body>";
echo "</html>";
4

3 回答 3

1

您可以检查查询的输出$record...

 if (count($record)==0) {
    echo "the ID you entered does not exist! Try again...";
 } else {   
    // code to output the part's details...
 }

if (count...零件代替...

 if($part_number == "")
于 2013-03-01T10:46:17.253 回答
1

您根本没有验证结果确实返回任何数据的任何地方。在调用 之后mysql_query(),您应该mysql_num_rows()立即查看查询返回了多少行——如果mysql_num_rows($result_set)为零,则您的查询没有返回任何数据。

请注意如何$part_number从未被或任何这些函数修改mysql_query()mysql_fetch_array()所以它永远不会是空的,除非它是这样开始的(使你的当前if几乎没用)。

于 2013-03-01T10:44:31.527 回答
0

从你的代码中我注意到两件事

$query = "select * from part where part_number = '" . $part_number . "'"; 

由于您的零件编号是一个字符串,我建议您不要LIKE使用=

$query = "select * from part where part_number LIKE '" . $part_number . "'";

另一个是检查您的记录是否以多维数组的形式返回,例如

 $record = Array([0]=>array('part_description'=>A123...)).

那么你必须像这样分配

$part_description = $record[0]['part_description'];

我希望它可以帮助你

于 2013-03-01T11:00:30.450 回答