If Statement Help

Hi. I’m new to this forum and this is my first post. That being said. I’m running into an issue where I’m using multiple “If” statements. After the tree of if statements, I have an “Else” statement. The way it’s suppose to work is If a condition is met, then the script stops. Let me give an example. We’re taking values from a submitted form

[php]

If ($_GET[‘device’] === “”)
{
echo ‘ERROR. You did not enter any data into the DEVICE field. Please make sure all fields have values.’;
echo ‘
’;
}

if ($_GET[‘employee’] ==="")
{
echo ‘ERROR. You did not enter any data into the EMPLOYEE field. Please make sure all fields have values.’;
echo ‘
’;
}

if ($_GET[‘datepicker1’] === “”)
{
echo ‘ERROR. You did not enter any data into the STARTING DATE field. Please make sure all fields have values.’;
echo ‘
’;
}

if ($_GET[‘timepicker1’] === “”)
{
echo ‘ERROR. You did not enter any data into the STARTING TIME field. Please make sure all fields have values.’;
echo ‘
’;
}

Else {

Connect to the database and write our values to the database. Last but not least, return a success confirmation message.

}
[/php]

The problem is, even if a field is detected blank, the error page warns the user that it’s blank, however it still executes the “Else” statement. The way I’m wanting it to function is, If a field is detected blank, then the user receives the error and the script STOPS. I know it’s something simple. This is just one of those tiny things that has me pulling my hair out. Any help?

I think your problem is your using 3 === signs this doesnt mean equal to you need to use two ==

Like zammit1234 said you should be using == instead of ===…

Right now you have three if() statements and one if-then-else statement

if you want to have four if() statements inside of an if-then-else you would need to do something like this…

[php]
if($_POST[‘submit’]){
if($_GET[‘device’] == “”){
echo ‘ERROR. You did not enter any data into the DEVICE field. Please make sure all fields have values.’;
echo ‘
’;
}

	if($_GET['employee'] ==""){
		echo 'ERROR. You did not enter any data into the EMPLOYEE field. Please make sure all fields have values.';
			echo '<br>';
	}
	
	if($_GET['datepicker1'] == ""){
		echo 'ERROR. You did not enter any data into the STARTING DATE field. Please make sure all fields have values.';
		echo '<br>';
	}
	
	if($_GET['timepicker1'] == ""){
		echo 'ERROR. You did not enter any data into the STARTING TIME field. Please make sure all fields have values.';
		echo '<br>';
	}
}
else {
	// Connect to the database and write our values to the database. Last but not least, return a success confirmation message.
}

[/php]

it looks like what you are trying to do is say if there are no errors we will upload to the database, otherwise we want to notify them of their errors. You can do this by setting a variable that will hold the error messages and setting a variable that will count the errors and if there are no errors it will upload to the database, otherwise it will display the error message. Example…
[php]
$errorMsg = “You have errors that need fixed”;
$errorCount = 0;

if($_GET['device'] === ""){
	$errorMsg .= '<br />ERROR. You did not enter any data into the DEVICE field. Please make sure all fields have values.';
	$errorCount++;
}

if($_GET['employee'] ===""){
	$errorMsg .= '<br />ERROR. You did not enter any data into the EMPLOYEE field. Please make sure all fields have values.';
	$errorCount++;
}

if($_GET['datepicker1'] === ""){
	$errorMsg .= '<br />ERROR. You did not enter any data into the STARTING DATE field. Please make sure all fields have values.';
	$errorCount++;
}

if($_GET['timepicker1'] === ""){
	$errorMsg .= '<br />ERROR. You did not enter any data into the STARTING TIME field. Please make sure all fields have values.';
	$errorCount++;
}

if($errorCount == 0) {
	// Connect to the database and write our values to the database. Last but not least, return a success confirmation message.
}
else {
	echo $errorMsg;
}

[/php]

This way the user will be able to see all errors that need to be fixed at one time instead of just one at a time…

Does this help you out err no?

Very informative. I’ve tried using “==” in the beginning and was not able to get it to work. So I moved on to “===”. I will try restructuring my if statement like you guys have recommended. I will let you know which works out for me. I really appreciate the help zammit1234 and mdahlke. I will report back here very shortly.

You only use === in JavaScript. PHP does not recognize the 3 ='s, you must only use ==.

Sorry for the delay. I had quite a bit at work going on. In any event I’ve tried both of your suggestions mdahlke. They both give me the same result as my original code. When a field is left blank on the form, it prompts the user with the error, however it still executes the entire script. I’ve been working with your second suggestion which is this …

[php]
$errorMsg = “You have errors that need fixed”;
$errorCount = 0;

if($_GET[‘device’] === “”){

$errorMsg .= ‘
ERROR. You did not enter any data into the DEVICE field. Please make sure all fields have values.’;

$errorCount++;

}

if($_GET[‘employee’] ===""){

$errorMsg .= ‘
ERROR. You did not enter any data into the EMPLOYEE field. Please make sure all fields have values.’;
$errorCount++;

}

if($_GET[‘datepicker1’] === “”){

$errorMsg .= ‘
ERROR. You did not enter any data into the STARTING DATE field. Please make sure all fields have values.’;
$errorCount++;
}

if($_GET[‘timepicker1’] === “”){

$errorMsg .= ‘
ERROR. You did not enter any data into the STARTING TIME field. Please make sure all fields have values.’;
$errorCount++;

}

if($errorCount == 0) {

// Connect to the database and write our values to the database. Last but not least, return a success confirmation message.
}
else {

echo $errorMsg;

}

=========== My database connection functions start here ==================================
[/php]

The database functions are still being executed. This should not be if there was a blank field. Again, this goes back to the same issue I had initially. I’m going to keep playing around with it, but if anymore suggestions come to mind, please post them here.

your database information should be in that last if() where the comment says…
[php]
// Connect to the database and write our values to the database. Last but not least, return a success confirmation message.
[/php]

You definitely deserve it. Today is my first day on this site. I’m am clearly new here. You’ll have to tell me where that feature is…

Sorry, my last response was in regards to your signature. I’m looking for a karma feature on this site.

You can’t hand out karma yet…you need more posts, but I am glad I can help.

BINGO GENIUS! This worked! Now, I’m just trying to under the logic in regards to why your method worked and my original method did not. Was it because I had multiple IF statements and no ELSE statements. Is it safe to say for every IF statement used in this case, there has to be a ELSE statement following it? Again, Thank you so much. I can’t believe i spent so much time on something this small. As you can tell, I’m very very rusty on my php coding. Some of it is slowly coming back, but there is much I’m still learning all over again.

Just want to point out that this is absolutely not true. Using === means that you are also matching the type.

For example:

[php]
0 === “0” // false because int 0 does not equal string 0
0 == “0” // true because a type conversion is done
[/php]

I personally ONLY use ===. I don’t like leaving the possibility of unknown type conversions.

http://php.net/manual/en/language.operators.comparison.php

I did not know that and I am glad you said something.
I only use === in JavaScript but was unaware of it being available in PHP, very good to know.

Thanks m@tt. I agree with you. Infact, I have used “===” many of times in php in regards to comparisons. I wasnt going to make a big stink about it so I never said anything. My overall focus was getting this to work correctly and understanding standing the resolution so it could be correctly applied in the future. That was accomplished, so I’m happy :smiley:

Sponsor our Newsletter | Privacy Policy | Terms of Service