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