点击这里</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屋!
查看全文