jrws Posted August 9, 2008 Share Posted August 9, 2008 Alright heres my problem, I am designing a website using my own code, I used the suggested error array error display. So what happens is no matter even if there are no errors when it is sent it still outputs all possible errors. Please help me rectify this. I am testing on local host ATM. Heres the code, and as far as I can tell it is perfectly fine. I was going to add more error checking but until I figure out why it doesn't work I have omitted it. <?php /** * Register page */ session_start(); include ("includes/functions.php"); if (isset($_POST['submit'])) { $error = array(); $user = protect($_POST['user']); $disname = protect($_POST['disname']); $pass = protect($_POST['pass']); $passcon = protect($_POST['passcon']); $email = protect($_POST['email']); $emailcon = protect($_POST['emailcon']); //Start Error checking if(!$user||!$disname||!$pass||!$passcon||!$email||!$emailcon){ $error[] = "One or more fields are not filled in, please try again.</span><br>"; } if(count($error) > 0) { echo "The following errors occured:</br>"; foreach($error as $err) { echo"<span style=\"color: red;\">$err"; } } else{ echo "Success!"; } } ?> <html> <body> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" href="c3cd01d0/main.css" type="text/css" media="screen"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Registration</title> </head> <p>All fields are required.</p> <form id="register" name="register" method="post" action="<? echo $HTTP_SERVER_VARS['PHP_SELF']; ?>"> <table width="292" border="0" cellspacing="0" cellpadding="3"> <tr> <th width="286" scope="row"><div align="left">Desired Username : <input name="user" type="text" maxlength="32" /> </div></th> </tr> <tr> <th scope="row"><div align="left">Display Name: <input name="disname" type="text" maxlength="64" /> </div></th> </tr> <tr> <th scope="row"><div align="left">Desired Password : <input name="pass" type="password" maxlength="32" /> </div></th> </tr> <tr> <th scope="row"><div align="left">Confirm Password : <input name="passcon" type="password" maxlength="32" /> </div></th> </tr> <tr> <th scope="row"><div align="left">Email: <input type="text" name="email" /> </div></th> </tr> <tr> <th scope="row"><div align="left">Confirm Email : <input type="text" name="emailcon" /> </div></th> <tr> <th scope="row"><div align="left">Confirm Code (Bot protection): <input type="text" name="code" /><?code()?> </div></th> </tr> </table> <input type="submit" value = "Submit" name="submit"> <input type="reset" name="Reset" value="Reset"> <p>Or perhaps you would like to<a href="http://sandstorm.net46.net/login.php"> login </a></p> </form> </body> </html> <? footer(); ?> //Protect code: function protect($String) { $String = trim($String); $String = mysql_real_escape_string($String); $String = addslashes($String); $String = strip_tags($String); $String = stripslashes($String); //return $String; } Quote Link to comment https://forums.phpfreaks.com/topic/118904-solved-errors-not-working/ Share on other sites More sharing options...
ratcateme Posted August 9, 2008 Share Posted August 9, 2008 i think your problem is that the return on the protect function is commented i think Scott. Quote Link to comment https://forums.phpfreaks.com/topic/118904-solved-errors-not-working/#findComment-612276 Share on other sites More sharing options...
Bendude14 Posted August 9, 2008 Share Posted August 9, 2008 Try this... see i made use of the empty($var) function i also put the ending span and <br /> into the foreach loop if (isset($_POST['submit'])) { $error = array(); $user = protect($_POST['user']); $disname = protect($_POST['disname']); $pass = protect($_POST['pass']); $passcon = protect($_POST['passcon']); $email = protect($_POST['email']); $emailcon = protect($_POST['emailcon']); //Start Error checking if(empty($user)||empty($disname)||empty($pass)||empty($passcon)||empty($email)||empty($emailcon)){ $error[] = "One or more fields are not filled in, please try again."; } if(count($error) > 0) { echo "The following errors occured:</br>"; foreach($error as $err) { echo"<span style=\"color: red;\">$err</span><br>"; } } else{ echo "Success!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/118904-solved-errors-not-working/#findComment-612281 Share on other sites More sharing options...
wildteen88 Posted August 9, 2008 Share Posted August 9, 2008 Your protect() function doesn't return a value and thus you get errors no matter what you fill your form with. function protect($String) { // check weather magic_quotes is enabled if(get_magic_quotes_gpc()) { $String = stripslashes($String); } // escape harmful characters $String = mysql_real_escape_string($String); // remove HTML tags $String = strip_tags($String); // in order for a function return anything you'll // need to use the 'return' keyword return trim($String); } I have reconstructed your function, you'll noticed I removed addslashes. You do not need to use this function, if you're using mysql_real_escape_string Quote Link to comment https://forums.phpfreaks.com/topic/118904-solved-errors-not-working/#findComment-612285 Share on other sites More sharing options...
jrws Posted August 9, 2008 Author Share Posted August 9, 2008 Thanks you guys Quote Link to comment https://forums.phpfreaks.com/topic/118904-solved-errors-not-working/#findComment-612634 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.