Caelan Grgurovic Caelan Grgurovic - 7 months ago 11
PHP Question

Recurring Error Display error function php

I have an

output_errors
function on my website which outputs all the "set" errors in a variable.

It pretty much works exactly how it should except for one thing; for one error in particular, it will output that error more than once (which it shouldn't).

How it is supposed to work is: if the user that is registering does not input any information into a certain part of the form, it needs to output (once) the error
Fields marked with an asterisk(*) must be filled in.
, along with any other errors that the user has come across. All of this is displayed in an unordered list.

This is the function that I have created:

function output_errors($errors){
return '<ul><li>' . implode('</li><li>', $errors) . '</li></ul>';
}


This is the code in which I specify when an error should be output:

$required = array('fname', 'username', 'password', 'password_again', 'email');
$reqCCNo = array('ccno');

// validation

foreach($_POST as $key=>$value){
if(empty($value) && in_array($key, $required) === true){
$errors[] = 'Fields marked with an asterisk(*) must be filled in.';
}
if(empty($value) && in_array($key, $reqCCNo) === true){
$errors[] = 'Please select a country.';
}
}

if(empty($errors)){

// credentials
if(preg_match('/[^a-z_\-0-9]/i', $fnp) || preg_match('/[^a-z_\-0-9]/i', $lnp)){
$errors[] = 'Credentials must only contain letters and numbers.';
}

// username
$result = mysqli_query($conn, "SELECT username FROM users WHERE username = '$user'");
$count = mysqli_num_rows($result);
if($count !== 0) {
$errors[] = 'That username is already taken.';
}
if(strlen($user) < 4){
$errors[] = 'Your username must be more than 4 characters long.';
}
if(strlen($user) > 16){
$errors[] = 'Your username must not be more than 16 characters long.';
}
if(preg_match('/[^a-z_\-0-9]/i', $user)){
$errors[] = 'Your username can only contain Alphanumeric characters.';
}

// email
if(filter_var($emailNex, FILTER_VALIDATE_EMAIL) === false){
$errors[] = 'That is not a valid email type.';
}
$email_result = mysqli_query($conn, "SELECT email FROM users WHERE email = '$emailNex'");
$email_count = mysqli_num_rows($email_result);
if($email_count !== 0) {
$errors[] = 'That email is already in use.';
}

// password
if(strlen($pass) < 6){
$errors[] = 'Your password must be more than 6 characters long.';
}
if($pass !== $_POST['password_again']){
$errors[] = 'Those passwords do not match!';
}

}


and, this is the code that I use to output all of those errors:

if(!empty($errors)){
echo output_errors($errors);
}


Say that I leave all the fields blank and input a username less than 4 characters long, this is how it should be output:

Fields marked with an asterisk(*) must be filled in.

Your username must be more than 4 characters long.

this is how it is being output right now:

Fields marked with an asterisk(*) must be filled in.

Fields marked with an asterisk(*) must be filled in.

Fields marked with an asterisk(*) must be filled in.

Please select a country.

Your username must be more than 4 characters long.

All help is appreciated!

Thanks

Answer

Problem is with your foreach loop. it insert error message for every Required file.

You need to create a flag outside your foreach loop and set it to true when it comes inside your condition as

$flag=FALSE;// set it false
foreach($_POST as $key=>$value){
    if(empty($value) && in_array($key, $required) === true){
        $flag=TRUE;// set true if fulfill your condition
    }
}
if($flag){// set your message
    $errors[] = 'Fields marked with an asterisk(*) must be filled in.';
}

It will set your error message once instead of multiple