Jump to content

problem with an if else statement


Howlin1

Recommended Posts

My code is as follows

<?php
// Include the Sessions options
require_once('inc/session_admin.inc.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Change Record form</title>
<style type="text/css">
td {font-family: tahoma, arial, verdana; font-size: 10pt }
</style>


</head>
<body bgcolor="#64b1ff">
<div align="center">
<?php
echo '<p>Hello <b>' .($_SESSION['username']) .'</b> <a href="logging_out.php">Logout</a></p>';

$Username=$_POST['Username'];

if ($Username = $Username)
{
// Include connecting to the database
require_once('inc/connect_to_users_db.inc.php');

$query=" SELECT * FROM userinfo WHERE Username='$Username'";
$result=mysql_query($query);
$num=mysql_num_rows($result);

$i=0;
while ($i < $num) {
$FirstName=mysql_result($result,$i,"FirstName");
$LastName=mysql_result($result,$i,"LastName");
$Address1=mysql_result($result,$i,"Address1");
$Address2=mysql_result($result,$i,"Address2");
$Address2=mysql_result($result,$i,"Address2");
$Address3=mysql_result($result,$i,"Address3");
$Gender=mysql_result($result,$i,"Gender");
$Country=mysql_result($result,$i,"Country");
$MobileNumber=mysql_result($result,$i,"MobileNumber");
$EmailAddress=mysql_result($result,$i,"EmailAddress");
?>
<table width="300" cellpadding="5" cellspacing="0" border="1">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
<h3>Edit and Submit</h3>
<form method="post" action="update_a_user_script.php">
<input type="hidden" name="update_a_user_script" value="<?php echo "$Username" ?>" />
<table cellpadding="0" cellspacing="0"  border="0">
<tr align="center" valign="top">
<td>Username:</td>
<td><input type="text" name="Username" readonly="readonly" value="<?php echo "$Username" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>First Name:</td>
<td><input type="text" name="FirstName" value="<?php echo "$FirstName" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Last Name:</td>
<td><input type="text" name="LastName" value="<?php echo "$LastName" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Address:</td>
<td><input type="text" name="Address1" value="<?php echo "$Address1" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td> </td>
<td><input type="text" name="Address2" value="<?php echo "$Address2" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td> </td>
<td><input type="text" name="Address3" value="<?php echo "$Address3" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Gender:</td>
<td><input type="text" name="Gender" value="<?php echo "$Gender" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Country:</td>
<td><input type="text" name="Country" value="<?php echo "$Country" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>MobileNumber:</td>
<td><input type="text" name="MobileNumber" value="<?php echo "$MobileNumber" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Email Address:</td>
<td><input type="text" name="EmailAddress" value="<?php echo "$EmailAddress" ?>" /></td>
</tr>
</table>
<input type="submit" value="Update" />
</form>
<form method="post" action="update_a_user_form.php">
<input type="submit" value="Update a Different Record" />
</form>
<form method="post" action="display_users.php">
<input type="submit" value="List all users" />
</form>
<form method="post" action="index.php">
<input type="submit" value="Administrator Interface" />
</form>
</td></tr></table>
<?php
++$i;
}
?>
<?php
}
else
{
echo 'Username not found!. Please try again';
?>
<table width="300" cellpadding="5" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="left" colspan="1" rowspan="1" bgcolor="#64b1ff">
<h3>Insert Username</h3>
<form method="post" action="update_a_user_form2.php">
Enter Username: <input type="text" name="Username" size="30" /><br />
<br />
<input type="submit" value="Submit" /><input type="reset" />
</form>
<form method="post" action="index.php">
<input type="submit" value="Administrator Interface" />
</form>
</td>
</tr>
</table>
<?php
}
?>
</div>
</body>
</html>

It will display what it's meant to display if I have a username that is in the database, however if the username doesn't exist in the database, it's meant to say "Username not found!. Please try again" and show the search box again. However if I put in a Username that doesn't exist. I get a blank page (well at the top of the page the page I do get Hello Howlin1 Logout)

 

What am I doing wrong?

Link to comment
Share on other sites

Ah Duh! Thanks  :)

I changed it to

<?php
echo '<p>Hello <b>' .($_SESSION['username']) .'</b> <a href="logging_out.php">Logout</a></p>';

// Include connecting to the database
require_once('inc/connect_to_users_db.inc.php');

$Username=$_POST['Username'];


$sql="SELECT * FROM userinfo WHERE Username='$Username'";
$result=mysql_query($sql) or die(mysql_error());

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $Username, table row must be 1 row

if ($count == "0")
{

echo 'Username not found!. Please try again';
?>
<table width="300" cellpadding="5" cellspacing="0" border="2">
<tr align="center" valign="top">
<td align="left" colspan="1" rowspan="1" bgcolor="#64b1ff">
<h3>Insert Username</h3>
<form method="post" action="update_a_user_form2.php">
Enter Username: <input type="text" name="Username" size="30" /><br />
<br />
<input type="submit" value="Submit" /><input type="reset" />
</form>
<form method="post" action="index.php">
<input type="submit" value="Administrator Interface" />
</form>
</td>
</tr>
</table>
<?php
}
else
{

$query=" SELECT * FROM userinfo WHERE Username='$Username'";
$result=mysql_query($query);
$num=mysql_num_rows($result);

$i=0;
while ($i < $num) {
$FirstName=mysql_result($result,$i,"FirstName");
$LastName=mysql_result($result,$i,"LastName");
$Address1=mysql_result($result,$i,"Address1");
$Address2=mysql_result($result,$i,"Address2");
$Address2=mysql_result($result,$i,"Address2");
$Address3=mysql_result($result,$i,"Address3");
$Gender=mysql_result($result,$i,"Gender");
$Country=mysql_result($result,$i,"Country");
$MobileNumber=mysql_result($result,$i,"MobileNumber");
$EmailAddress=mysql_result($result,$i,"EmailAddress");
?>
<table width="300" cellpadding="5" cellspacing="0" border="1">
<tr align="center" valign="top">
<td align="center" colspan="1" rowspan="1" bgcolor="#64b1ff">
<h3>Edit and Submit</h3>
<form method="post" action="update_a_user_script.php">
<input type="hidden" name="update_a_user_script" value="<?php echo "$Username" ?>" />
<table cellpadding="0" cellspacing="0"  border="0">
<tr align="center" valign="top">
<td>Username:</td>
<td><input type="text" name="Username" readonly="readonly" value="<?php echo "$Username" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>First Name:</td>
<td><input type="text" name="FirstName" value="<?php echo "$FirstName" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Last Name:</td>
<td><input type="text" name="LastName" value="<?php echo "$LastName" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Address:</td>
<td><input type="text" name="Address1" value="<?php echo "$Address1" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td> </td>
<td><input type="text" name="Address2" value="<?php echo "$Address2" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td> </td>
<td><input type="text" name="Address3" value="<?php echo "$Address3" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Gender:</td>
<td><input type="text" name="Gender" value="<?php echo "$Gender" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Country:</td>
<td><input type="text" name="Country" value="<?php echo "$Country" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>MobileNumber:</td>
<td><input type="text" name="MobileNumber" value="<?php echo "$MobileNumber" ?>" /></td>
</tr>
<tr align="center" valign="top">
<td>Email Address:</td>
<td><input type="text" name="EmailAddress" value="<?php echo "$EmailAddress" ?>" /></td>
</tr>
</table>
<input type="submit" value="Update" />
</form>
<form method="post" action="update_a_user_form.php">
<input type="submit" value="Update a Different Record" />
</form>
<form method="post" action="display_users.php">
<input type="submit" value="List all users" />
</form>
<form method="post" action="index.php">
<input type="submit" value="Administrator Interface" />
</form>
</td></tr></table>
<?php
++$i;
}
}
?>

and it works! Thanks a lot!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.