6 Replies - 426 Views - Last Post: 29 April 2013 - 03:00 PM Rate Topic: -----

#1 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 129
  • Joined: 30-January 13

quick question on syntax of if and if else statement

Posted 29 April 2013 - 02:24 PM

I am making a home page that allows the user to sign in to a members only page. I want to set it up to where if they click submit without typing anything it will tell them they must fill in all fields, and if they use an incorrect username or password it will tell them that as well. I am having some trouble figuring out the syntax to make this happen though. I have it to where it will tell them if they leave username and password blank that they must fill in both fields, but it will not tell them when they have entered something incorrect. It will just cycle back to the page and clear what they typed but throws the same error as if they would have not typed anything at all. I have posted my code below with out the error for typing an incorrect username and password because this is the part I am having trouble with. I have tried several different ways and in several different locations in the code. If someone could tell me the proper way to write just this part and the location to put it I would appreciate it greatly. I have been trying several variations of code such as
else{
$strMsg="Your username and password do not match our records.";
		}
and using if else statements in different locations of the code but I seem to be so close yet so far.
<?php
include("config.php");

if ($_POST['username']=="" || $_POST['password']==""){
		$strMsg="You must enter your user name and password ";
		}
if($_POST['submit']!=""){
	$db=dbConnect();
	$SQL="SELECT userID FROM tblusers WHERE username='".$_POST['uName']."'";

	$rs=mysql_query($SQL);

	if(mysql_num_rows($rs)>0 && $rs){

		$SQL="SELECT userID FROM tblusers WHERE PASSWORD=PASSWORD

		('".$_POST['pWord']."') AND userID=".

		mysql_result($rs,0,"userID");

		//echo $SQL;
		$rs=mysql_query($SQL);

		if(mysql_num_rows($rs)>0 && $rs){
			header("Location:memberContent.php");

		}
	}
}


?>
<!--  I am a comment -->
<html>
	<head>
		<title>Sign in!</title>
	</head>
	<body>
	<p><a href="member_register.php">Sign Up!</a></p>
	<h1>Please enter your username and password!</h1>
		<?php echo $strMsg;?>
		<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
			Username: <input type="text" name="uName" />
			<br />
			Password: <input type="password" name="pWord" />
			<br />
			<input type="submit" name="submit" value="sign in" />
		</form>
	</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: quick question on syntax of if and if else statement

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3475
  • View blog
  • Posts: 11,812
  • Joined: 12-December 12

Re: quick question on syntax of if and if else statement

Posted 29 April 2013 - 02:36 PM

if ($_POST['username']=="" || $_POST['password']==""){
		$strMsg="You must enter your user name and password ";
		}
if($_POST['submit']!=""){

Notice that you will still attempt to connect to the database even if they haven't filled in a username or password, so you need an else-clause. If your not sure how then revise this topic. (I would also re-order these statements, only attempting to check the username/password if this is a form submission.)

I would initialise $strMsg = false;. Then, when you construct the form, check if this is still false; if it is, then embed the submitted username and password into the form.

However, I wouldn't embed either; or, if pushed, just embed the username, not the password.

The mysql library is DEPRECATED - see my signature.

This post has been edited by andrewsw: 29 April 2013 - 02:38 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3475
  • View blog
  • Posts: 11,812
  • Joined: 12-December 12

Re: quick question on syntax of if and if else statement

Posted 29 April 2013 - 02:43 PM

Actually, the else is not essential, just the order and logic of the code needs to be reconsidered.

$strMsg = false;

if (isset($_POST['username'])) {
    // this is a form submission
}

Was This Post Helpful? 0
  • +
  • -

#4 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 129
  • Joined: 30-January 13

Re: quick question on syntax of if and if else statement

Posted 29 April 2013 - 02:44 PM

Thank you for the reply. I see the issue with trying to connect to the database however I am afraid I do not know what an else clause is or how to use one.

View Postandrewsw, on 29 April 2013 - 04:43 PM, said:

Actually, the else is not essential, just the order and logic of the code needs to be reconsidered.

$strMsg = false;

if (isset($_POST['username'])) {
    // this is a form submission
}

Thanks I am trying to figure out how to use this but I appriciate the help.
Was This Post Helpful? 0
  • +
  • -

#5 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 129
  • Joined: 30-January 13

Re: quick question on syntax of if and if else statement

Posted 29 April 2013 - 02:49 PM

I tried to add
$strMsg = false;

if (isset($_POST['username'])) {
    }
to the top of my code but that is not doing it not sure if I did not do something correctly.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3475
  • View blog
  • Posts: 11,812
  • Joined: 12-December 12

Re: quick question on syntax of if and if else statement

Posted 29 April 2013 - 02:53 PM

The following may also help:

$strMsg = false;

if (isset($_POST['username'])) {
    // this is a form submission
    $uname = $_POST['username'];
}

// then, for the form:
        Username: <input type="text" name="uName"
        value="<?php echo ($strMsg === false) ? '' : $uname; ?>" />


View Postak4744710, on 29 April 2013 - 09:49 PM, said:

I tried to add
$strMsg = false;

if (isset($_POST['username'])) {
    }
to the top of my code but that is not doing it not sure if I did not do something correctly.

Yes, you need to incorporate this into your code, not just copy and paste (and hope) :dontgetit:

This post has been edited by andrewsw: 29 April 2013 - 02:51 PM

Was This Post Helpful? 0
  • +
  • -

#7 ak4744710  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 129
  • Joined: 30-January 13

Re: quick question on syntax of if and if else statement

Posted 29 April 2013 - 03:00 PM

tried like this
<?php
include("config.php");
$strMsg = false;

if (isset($_POST['username'])) {
    // this is a form submission
    $uname = $_POST['username'];
}

if($_POST['submit']!=""){
	$db=dbConnect();
	$SQL="SELECT userID FROM tblusers WHERE username='".$_POST['uName']."'";

	$rs=mysql_query($SQL);

	if(mysql_num_rows($rs)>0 && $rs){

		$SQL="SELECT userID FROM tblusers WHERE PASSWORD=PASSWORD

		('".$_POST['pWord']."') AND userID=".

		mysql_result($rs,0,"userID");

		//echo $SQL;
		$rs=mysql_query($SQL);

		if(mysql_num_rows($rs)>0 && $rs){
			header("Location:memberContent.php");

		}

	}
}


?>
<!--  I am a comment -->
<html>
	<head>
		<title>Sign in!</title>
	</head>
	<body>
	<p><a href="member_register.php">Sign Up!</a></p>
	<h1>Please enter your username and password!</h1>
		<?php echo $strMsg;?>
		<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

			        Username: <input type="text" name="uName"
        value="<?php echo ($strMsg === false) ? '' : $uname; ?>" />

			<br />
			Password: <input type="password" name="pWord" />
			<br />
			<input type="submit" name="submit" value="sign in" />
		</form>
	</body>
</html>
then I saw your last comment so i think it may be becuase I needed to add that code not substitute with it thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1