Jump to content

[SOLVED] While Loop statment problem..


garyit

Recommended Posts

Dea friends,

 

I have facing difficult with the while-loop statement.

I want it to return error msg when the result is false, but it couldn't work!

 

Below is the code:

<?php
if(session_is_registered('user_name'))
{
  echo '<blockquote><p align="center"><img src="img/TopBanner02.jpg" width="1024" height="100" />
  <p></p>

  <table width="200" border="0" align="center">
    <tr>
      <td><a href="newrec.php"><img src="img/addnewbtn.jpg" width="146" height="38" border="0" /></td>
      <td><a href="delrec.php"><img src="img/deletebtn.jpg" width="146" height="39" border="0" /></td>
      <td><a href="updaterec.php"><img src="img/updatebtn.jpg" width="146" height="39" border="0" /></td>
      <td><a href="searchrec.php"><img src="img/searchbtn.jpg" width="146" height="39" border="0" /></td>
    </tr>
  </table>


  <p align="center" class="style1"><u>Records Detail</u>
  <p></p></blockquote>';

   	define ('DB_USER', '');
	define ('DB_PASSWORD', '');
	define ('DB_HOST', 'localhost');
	define ('DB_NAME', 'regent');

    $dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not
	connect to MySQL: ' . mysql_error() );
	mysql_select_db (DB_NAME) OR die ('Could not select database: ' .
	mysql_error() );

    $result = mysql_query("SELECT * FROM guest WHERE guest_room=$guest_room");


    while($row = mysql_fetch_array($result))
    {
    //getting each variable from the table
    $record_id = $row['record_id'];
    $date = $row['day'].'-'.$row['month'].'-'.$row['year'];
    //$self = $_SERVER['PHP_SELF'];

    echo "<table width='61.5%' height='496' border='1' align='center' cellpadding='0' cellspacing='0' bordercolor='#808080' bgcolor='#FFFFCC' id='AutoNumber1' style='border-collapse: collapse'>
    <tr>
      <td width='17%' height='33'><div align='center'><font size='2'><b>Guest room #:</b></font>
      </div></td>
      <td width='83%' height='33' align='left'>  
          <input name='guest_room' type='text' disabled size='4' value=" . $row['guest_room'] ." maxlength='4' tab='1' />
    </tr>";

    echo "
    <tr>
      <td width='17%' height='34'><div align='center'><b><font size='2'>Date: </font> </b></div></td>
      <td width='83%' height='34'>
        <div align='left'>  
          <input name='date' type='text' disabled id='date'  size='10' value=" . $row['date'] ."  >
    </tr>";

    echo "
    <tr>
      <td width='17%' height='36'><div align='center'><b><font size='2'>Time: </font> </b></div></td>
      <td width='83%' height='36'><div align='left'>  
        <input name='time' type='text' disabled id='time'  value=" . $row['time'] ,  $row['ampm'] ." size='10'>
         </div></td>
    </tr>";

    echo "
    <tr>
      <td width='17%' height='141'><div align='center'><b><font size='2'>Issue:  </font></b></div></td>
      <td width='83%' height='141'>  
          <textarea name='issue' cols='55' rows='8' disabled tab='8'>". $row['issue'] ."</textarea>
      </td>
    </tr>";

    echo "
    <tr>
      <td width='17%' height='142'><div align='center'><b><font size='2'>Remark:   </font></b></div></td>
      <td width='83%' height='142'>  
          <textarea name='remark' cols='55' rows='8' disabled tab='9'>". $row['remark'] ."</textarea>
      </td>
    </tr> ";

    echo "
    <tr>
      <td width='17%' height='38'><div align='center'><b><font size='2'>Follow up:</font></b></div></td>
      <td width='83%' height='38'>  
      <input name='follow_up' type='text' disabled id='follow_up' value=". $row['follow_up'] ." size='4'></td>
    </tr>";

    echo"
    <tr>
      <td height='35'><div align='center'><b><font size='2'>Assigned to:</font></b></div></td>
      <td height='35' colspan='2'>   <input name='assigned_to' type='text' disabled id='assigned_to' value=". $row['assigned_to'] ." size='15' tab='11' /></td>
    </tr>";

    echo"
    <tr>
      <td height='35'><div align='center'><b><font size='2'>Date Resolved:</font></b></div></td>
      <td height='35' colspan='2'>  
        <input name='date_resolved' type='text' disabled id='date_resolved' size='10' value=". $row['date_resolved'] ." >
      </td>
    </tr>";
    echo "<p></p><p></p>";
    echo "</table>";
   //add HERE!
    }

   else
   {
   echo '<blockquote><p align="center">no record found!</p></blockquote>';
   }
  }


else
{
echo '<blockquote><p align="center">Your login session is expired.</br>Please <a href="gsl.htm">LOGIN</a> again</p></blockquote>';
}

?>

 

it complaint the line

   else
   {
   echo '<blockquote><p align="center">no record found!</p></blockquote>';
   }

 

If i remove the line it complain, then my script can be run with record input by user which is exist in database, but if the user supply

invalid record into it, it couldn't tell the user that record is not in database, this is the msg i want to display.

 

Thank You for HELP!

I really appreaciate it!

Link to comment
https://forums.phpfreaks.com/topic/37987-solved-while-loop-statment-problem/
Share on other sites

while does not have an else.

Try this: (I also fixed a few other things for you)

<?php
if(isset($_SESSION['user_name'])){
?>
<blockquote><p align="center"><img src="img/TopBanner02.jpg" width="1024" height="100" /><p></p>
<table width="200" border="0" align="center">
    	<tr>
		<td><a href="newrec.php"><img src="img/addnewbtn.jpg" width="146" height="38" border="0" /></td>
		<td><a href="delrec.php"><img src="img/deletebtn.jpg" width="146" height="39" border="0" /></td>
		<td><a href="updaterec.php"><img src="img/updatebtn.jpg" width="146" height="39" border="0" /></td>
		<td><a href="searchrec.php"><img src="img/searchbtn.jpg" width="146" height="39" border="0" /></td>
	</tr>
</table>
<p align="center" class="style1"><u>Records Detail</u>
<p></p></blockquote>
<?php
   	define ('DB_USER', '');
define ('DB_PASSWORD', '');
define ('DB_HOST', 'localhost');
define ('DB_NAME', 'regent');

$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error());
mysql_select_db (DB_NAME) OR die ('Could not select database: ' .mysql_error() );

$result = mysql_query("SELECT * FROM guest WHERE guest_room=$guest_room") OR die ('MYSQL Error:: ' .mysql_error() );
if(mysql_num_rows($result)){
	while($row = mysql_fetch_array($result)){
		//getting each variable from the table
		$record_id = $row['record_id'];
		$date = $row['day'].'-'.$row['month'].'-'.$row['year'];
		//$self = $_SERVER['PHP_SELF'];

		echo "<table width='61.5%' height='496' border='1' align='center' cellpadding='0' cellspacing='0' bordercolor='#808080' bgcolor='#FFFFCC' id='AutoNumber1' style='border-collapse: collapse'>
		<tr>
		<td width='17%' height='33'><div align='center'><font size='2'><b>Guest room #:</b></font>
		</div></td>
		<td width='83%' height='33' align='left'>  
		<input name='guest_room' type='text' disabled size='4' value=" . $row['guest_room'] ." maxlength='4' tab='1' />
		</tr>";

		echo "
		<tr>
		<td width='17%' height='34'><div align='center'><b><font size='2'>Date: </font> </b></div></td>
		<td width='83%' height='34'>
		<div align='left'>  
		<input name='date' type='text' disabled id='date'  size='10' value=" . $row['date'] ."  >
		</tr>";

		echo "
		<tr>
		<td width='17%' height='36'><div align='center'><b><font size='2'>Time: </font> </b></div></td>
		<td width='83%' height='36'><div align='left'>  
		<input name='time' type='text' disabled id='time'  value=" . $row['time'] ,  $row['ampm'] ." size='10'>
		 </div></td>
		</tr>";

		echo "
		<tr>
		<td width='17%' height='141'><div align='center'><b><font size='2'>Issue:  </font></b></div></td>
		<td width='83%' height='141'>  
		<textarea name='issue' cols='55' rows='8' disabled tab='8'>". $row['issue'] ."</textarea>
		</td>
		</tr>";

		echo "
		<tr>
		<td width='17%' height='142'><div align='center'><b><font size='2'>Remark:   </font></b></div></td>
		<td width='83%' height='142'>  
		<textarea name='remark' cols='55' rows='8' disabled tab='9'>". $row['remark'] ."</textarea>
		</td>
		</tr> ";

		echo "
		<tr>
		<td width='17%' height='38'><div align='center'><b><font size='2'>Follow up:</font></b></div></td>
		<td width='83%' height='38'>  
		<input name='follow_up' type='text' disabled id='follow_up' value=". $row['follow_up'] ." size='4'></td>
		</tr>";

		echo"
		<tr>
		<td height='35'><div align='center'><b><font size='2'>Assigned to:</font></b></div></td>
		<td height='35' colspan='2'>   <input name='assigned_to' type='text' disabled id='assigned_to' value=". $row['assigned_to'] ." size='15' tab='11' /></td>
		</tr>";

		echo"
		<tr>
		<td height='35'><div align='center'><b><font size='2'>Date Resolved:</font></b></div></td>
		<td height='35' colspan='2'>  
		<input name='date_resolved' type='text' disabled id='date_resolved' size='10' value=". $row['date_resolved'] ." >
		</td>
		</tr>";
		echo "<p></p><p></p>";
		echo "</table>";
	}
}else{
	echo '<blockquote><p align="center">no record found!</p></blockquote>';
}
}else{
echo '<blockquote><p align="center">Your login session is expired.</br>Please <a href="gsl.htm">LOGIN</a> again</p></blockquote>';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.