Jump to content

Recommended Posts

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;
}

Link to comment
https://forums.phpfreaks.com/topic/118904-solved-errors-not-working/
Share on other sites

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!";
}
}

?>

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

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.