nvee Posted February 22, 2010 Share Posted February 22, 2010 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 { ?> Quote Link to comment Share on other sites More sharing options...
eugene2009 Posted February 22, 2010 Share Posted February 22, 2010 you forgot the } at the last line Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 22, 2010 Share Posted February 22, 2010 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 } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.