检查并返回数据库中的值

时间:2014-12-02 21:00:46

标签: php mysql

我有这个小问题:

我想从数据库中检索一些行,它检索得很好,但是当我在文本字段中插入一个特定的值而不是要跟踪的数据库中时,它返回一个空/空白页面,下面是代码我用过:

<form id="track" name="track" method="post" action="track_now.php"> 
        <h2>Track your shipment Here</h2>

      <p><label> Tracking Reference: 
      <input type="text" id="reference" name="reference" value="" maxlength="40" required="required" /></label></p>


      <div class="button_holder">

        <p>   <input type="submit" id="track" value="Track Now" maxlength="40" required="required" /></label>
      </label></p>

      </div>
</form>

这是track_now.php

<form id="track" name="track" method="post" action=""> 
        <h2>Your Shipment Result</h2>

            <?php
//error_reporting(0);
$ref = mysql_real_escape_string($_POST['reference']);

// conmnecting to the database
if(isset($ref))
{ 
$db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database")); 
mysql_select_db('tracking') or die(mysql_error());

 $sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' "; 

$rs  = mysql_query($sql);
if($row = mysql_fetch_array($rs)) {

echo '<table width="518" border="1";>'; 

         echo '<tr>';
 echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Reference: </td>';
  echo '<td width="365" style="background-color:#fcfcfc; padding: 10px;  font-size:12px;">' . $row['ship_ref'] . "<br />" . '</td>'; 
 echo '</tr>';

     echo '<tr>';
 echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Type: </td>';
 echo '<td width="365" style="background-color:#fcfcfc; padding: 10px;  font-size:12px;">' . $row['ship_type'] . "<br />" . '</td>'; 
 echo '</tr>';

}

echo "</table>";
}
else if ($rs != $row) {
print 'Invalid Tracking Number, Please <a href="tracking.php"> click here </a> to try  again' ;
}

mysql_close();
?>    

拜托,我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

您的情况可以简化,请尝试这种方式:

if(isset($ref)){ 
    $db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database")); 
    mysql_select_db('tracking') or die(mysql_error());

    $sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' "; 
    $rs  = mysql_query($sql);
    if(!rs){
        die(mysql_error());
    }

    if($row = mysql_fetch_array($rs)) {
        echo '<table width="518" border="1";>'; 

        echo '<tr>';
        echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Reference: </td>';
        echo '<td width="365" style="background-color:#fcfcfc; padding: 10px;  font-size:12px;">' . $row['ship_ref'] . "<br />" . '</td>'; 
        echo '</tr>';

        echo '<tr>';
        echo '<td width="137" style="font-size:12px; padding: 5px;" >Shipment Type: </td>';
        echo '<td width="365" style="background-color:#fcfcfc; padding: 10px;  font-size:12px;">' . $row['ship_type'] . "<br />" . '</td>'; 
        echo '</tr>';
        echo "</table>";
    }
    mysql_close();
}
else{
    print 'Invalid Tracking Number, Please <a href="tracking.php"> click here </a> to try  again' ;
}

因为如果不满足第一个条件,else if ($rs != $row)将具有未定义的值。

正如@marco所指出的那样,你可以在没有抓取的情况下检查行:

if(mysql_num_rows($rs) > 0){
  //found a row
}