从数据库检查和返回值 [英] Checking and Returning values from database

查看:36
本文介绍了从数据库检查和返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题:

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

<h2>在此处跟踪您的货件</h2><p><标签>跟踪参考:<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>

</表单>

这是 track_now.php

<h2>您的发货结果</h2><?php//error_reporting(0);$ref = mysql_real_escape_string($_POST['reference']);//连接到数据库if(isset($ref)){$db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database"));mysql_select_db('tracking') 或 die(mysql_error());$sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' ";$rs = mysql_query($sql);if($row = mysql_fetch_array($rs)) {echo '';echo '<td width="137" style="font-size:12px; padding: 5px;">发货参考:</td>';echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">'.$row['ship_ref'] .<br/>".'</td>';回声'</tr>';echo '';echo '<td width="137" style="font-size:12px; padding: 5px;">货件类型:</td>';echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">'.$row['ship_type'] .<br/>".'</td>';回声'</tr>';}echo "</table>";}否则如果 ($rs != $row) {打印'无效的跟踪号,请点击这里</a>再试一次';}mysql_close();?>

请问,我在这里做错了什么?

解决方案

你的情况可以简化,试试这个:

if(isset($ref)){$db = mysql_connect('localhost', 'admin', "admin") or die(mysql_error("Cannot Connect to Database"));mysql_select_db('tracking') 或 die(mysql_error());$sql = "SELECT * FROM order_tracking WHERE ship_ref = '".$ref."' ";$rs = mysql_query($sql);如果(!rs){死(mysql_error());}if($row = mysql_fetch_array($rs)) {echo '
';echo '<td width="137" style="font-size:12px; padding: 5px;">发货参考:</td>';echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">'.$row['ship_ref'] .<br/>".'</td>';回声'</tr>';echo '';echo '<td width="137" style="font-size:12px; padding: 5px;">货件类型:</td>';echo '<td width="365" style="background-color:#fcfcfc; padding: 10px; font-size:12px;">'.$row['ship_type'] .<br/>".'</td>';回声'</tr>';echo "</table>";}mysql_close();}别的{打印'无效的跟踪号,请点击这里</a>再试一次';}

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

正如@marco 指出的,您可以在不获取的情况下检查行:

if(mysql_num_rows($rs) > 0){//找到一行}

I have this little issue:

I want to retrieve some rows from the database, it retrieves fine, but when i insert a particular value in the text field which is not present in the database to track, it returns an empty/blank page, below is the code i used:

<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>

and this is the 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();
?>    

Please, what am i doing wrong here?

Your condition could be simplified, try this way:

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' ;
}

because else if ($rs != $row) will have undefined value if first condition is not met.

As @marco pointed out, you can check for rows without fetching:

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

这篇关于从数据库检查和返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆