Jump to content

if else not working


nvee

Recommended Posts

Hey guys

 

The code below does not give any errors, but the problem comes in with the final if/else statement. What needs to happen is that if $error != "" (so meaning if there is errors) if must output the errors, else it must add the new group into the database.

 

It appears to skip that part, and irrelevant if there is errors, it continues to write the items to the database. Im sure its just a logic error, can anyone help?

 

<?php
		if($_POST["addgroup"] == "ADD") {
			$name = htmlentities($_POST["name"]);
			if(empty($name)) {
				$error .= "<li>No name was entered, please try again</li>";
			}
			if(!is_string($name)) {
			$error .= "<li>The name contained illegal characters. The group names can only contain text characters and no special characters or numbers. Please try again</li>";
			}
			connectdb();
			$name = mysql_escape_string($name);
			$query = mysql_query("SELECT * FROM groups WHERE name = '".$name."'");
			if(!$query) {
			trigger_error("There has been a problem with the database:" . mysql_error());	
			}
			$rows = mysql_num_rows($query);
			if(rows > 0) {
				$error .= "<li>There is already a group with the name ".$name." in the database. Try a different name</li>";	
			}
			if($error != "") {
					  echo "<h3>GROUP MANAGER</h3>";
					  echo "<p>The following errors has occured. Please <a href='newgroup.php'>click here</a> to try again</p>";
					  echo "<ul>";
					  echo $error;
					  echo "</ul>";
					  } else {
						  $query = mysql_query("INSERT INTO groups (`name`) VALUES ('$name')");
						  if(!$query) {
							trigger_error("There has been a problem inserting the data into the database: ".mysql_error());  
						  }
						  echo "<h3>GROUP MANAGER</h3>";
						  echo "<p>The group named ".$name." has successfully been entered into the database. <a href='groups.php'>Click here</a> to return to the groups page</p>";
					  }
		} else {
			?>

Link to comment
https://forums.phpfreaks.com/topic/192909-if-else-not-working/
Share on other sites

If you follow your logic, you check for errors at the start, then close that logic loop and do the insert anyway.

 

The insert needs to be in an conditional branch that only gets executed if no errors have occured

if($_POST["addgroup"] == "ADD") 
{
$error = 0;
$name = htmlentities($_POST["name"]);
if(empty($name)) 
{
	$error .= "<li>No name was entered, please try again</li>";
	$error = 1;
}
if(!is_string($name)) 
{
	$error .= "<li>The name contained illegal characters. The group names can only contain text characters and no special characters or numbers. Please try again</li>";
	$error = 1;
}
if(!$error)
{
	connectdb();
	...
	...
	...
	your insert code here
}
}

Link to comment
https://forums.phpfreaks.com/topic/192909-if-else-not-working/#findComment-1016016
Share on other sites

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.