2014-12-02 67 views
1

返回值我有這個小問題:檢查,並從數據庫

我想從數據庫中檢索某些行,它檢索很好,但是當我插入的文本字段中的特定值,它是不存在的數據庫來跟蹤,它返回一個空/空白頁,下面是我使用的代碼:

<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(); 
?>  

拜託,我是什麼錯在這裏做什麼?

+0

你的意思是通過插入一個特定值到字段是不存在的數據庫? – 2014-12-02 21:03:07

+2

使用mysqli或PDO ..不再維護mysql_functions。此外,您不會像這樣關閉表單: – alda1234 2014-12-02 21:04:47

+2

我沒有看到名爲「reference」的表單元素。我也沒有看到一個關閉''標籤。 – 2014-12-02 21:05:10

回答

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 
} 
+0

大家好,謝謝你的回覆。這是場景。用戶來到網站追蹤一些事情。他插入一個引用並單擊軌道,ref編號返回數據庫中的行。這工作正常,但我想要的情況下,如果插入一個錯誤的數字,一個警告消息,如:無效的號碼/錯誤的號碼應該彈出。我編輯了包含表單元素的代碼。此外,我已關閉窗體,仍然沒有改變..如何以及我在哪裏使用PDO對象?當我使用別的,Dreamweaver給了我代碼提示錯誤。 – 2014-12-03 05:57:29

+0

出於好奇心,爲什麼你在發佈問題後立即消失9小時? – meda 2014-12-03 06:05:59

+0

hello @Meda,感謝代碼,不幸的是,這些代碼拋出了一個未定義常量rs的使用 - 在C:\ wamp錯誤中假定'rs'。 我在這裏做錯了什麼? – 2014-12-03 06:06:25