需要帮助验证MySQL内部的数据

时间:2013-03-01 10:37:15

标签: php sql if-statement conditional-statements exists

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

示例:如果我为ID输入AX12,它将显示该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>";

3 个答案:

答案 0 :(得分:1)

您无法验证结果确实返回任何数据的任何位置。在您致电mysql_query()之后,您应该使用mysql_num_rows()查看您的查询返回了多少行 - 如果mysql_num_rows($result_set)为零,则您的查询未返回任何数据。

请注意$part_numbermysql_query()或其中任何一项功能永远不会修改mysql_fetch_array()的内容;所以它永远不会是空的,除非它是这样开始的(使你当前的if几乎没用)。

答案 1 :(得分: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 == "")

答案 2 :(得分:0)

从您的代码我注意到2件事

$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'];

我希望它可以帮助你