-1

I don't understand why I have implemented control instructions in my form that don't work, every time they record in the database all the behaviors that I don't want.

 /*ERRORI*/
    $nome_obbligatorio = "";
    $errore_nome = "";

    $cognome_obbligatorio = "";
    $errore_cognome = "";

    $password_obbligatoria = "";
    $due_psw_non_coincidono = "";
    $password_err_min_8_caratteri = "";
    $password_err_min_8_max_20_caratteri = "";
    $errore_password = "";

    $citta_obbligatorio = "";
    $errore_citta = "";

    $cap_obbligatorio = "";
    $errore_cap = "";
    $errore_cap_caratteri_speciali = "";

    $telefono_obbligatorio = "";
    $errore_telefono = "";

    $iban_obbligatorio = "";
    $iban_err_min_27_caratteri = "";

    /*FINE ERRORI*/

    if(isset($_POST['submit'])) {

        /*CAMPI FORM*/
        $nome = $connessione->real_escape_string($_POST['nome']);
        $cognome = $connessione->real_escape_string($_POST['cognome']);
        $password = $connessione->real_escape_string($_POST['password']);
        $citta = $connessione->real_escape_string($_POST['citta']);
        $cap = $connessione->real_escape_string($_POST['cap']);
        $telefono = $connessione->real_escape_string($_POST['telefono']);
        $iban = $connessione->real_escape_string($_POST['iban']);
        $email = $connessione->real_escape_string($_POST['email']);
        $confermaPassword = $connessione->real_escape_string($_POST['confermaPassword']);
        $indirizzo = $connessione->real_escape_string($_POST['indirizzo']);
        $indirizzo_txt = $connessione->real_escape_string($_POST['indirizzo_txt']);
        $civico = $connessione->real_escape_string($_POST['civico']);
        $codice_fiscale = $connessione->real_escape_string($_POST['codice_fiscale']);
        $categoria_richiesta = $connessione->real_escape_string($_POST['categoria_richiesta']);


        /*FINE CAMPI FORM*/
        /*----------------------------------------------------*/


        if (empty($_POST["nome"])) {
            $nome_obbligatorio = "Nome è un campo obbligatorio";
        } else {
            if (!preg_match("~[0-9]+~", $nome)) {
                $errore_nome = "Il nome non può contenere numeri";
            }
        }


        if (empty($_POST["cognome"])) {
            $cognome_obbligatorio = "Cognome è un campo obbligatorio";
        } else {
            if (!preg_match("~[0-9]+~", $cognome)) {
                $errore_cognome = "Il cognome non può contenere numeri";
            }
        }



        if (empty($_POST["password"])) {
            $password_obbligatoria = "Password è un campo obbligatorio";
        } else {
            if( strlen($password ) < 8 )
            {
                $password_err_min_8_caratteri .= "La Password deve contenere almeno 8 caratteri";

            }

            if( strlen($password ) > 20 )
            {
                $password_err_min_8_max_20_caratteri .= "La Password può essere composta da 8 fino a 20 caratteri";
            }

            if( !preg_match("A(?=[-_a-zA-Z0-9]*?[A-Z])(?=[-_a-zA-Z0-9]*?[a-z])(?=[-_a-zA-Z0-9]*?[0-9])[-_a-zA-Z0-9]{6,}z", $password ) )
            {
                $errore_password .= "La Password deve contenere almeno una lettera maiuscola una minuscola e un numero";
            }
        }


        if (empty($_POST["citta"])) {
            $citta_obbligatorio = "Città è un campo obbligatorio";
        } else {
            if (!preg_match("#\W+#", $citta)) {
                $errore_citta = "Città non può contenere caratteri speciali";
            }
        }


        if (empty($_POST["cap"])) {
            $citta_obbligatorio = "CAP è un campo obbligatorio";
        } else {
            if(!preg_match("#[a-z]+#", $cap ))
            {
                $errore_cap .= "Il CAP non può contenere lettere";
            }
            if( !preg_match("#\W+#", $cap ) )
            {
                $errore_cap_caratteri_speciali .= "Il CAP non può contenere caratteri speciali";
            }
        }


        if (empty($_POST["telefono"])) {
            $telefono_obbligatorio = "Telefono è un campo obbligatorio";
        } else {

            if( !preg_match("#[a-z]+#", $telefono ) )
            {
                $errore_telefono .= "Il telefono non può contenere lettere";
            }
        }


        if (empty($_POST["iban"])) {
            $iban_obbligatorio = "IBAN è un campo obbligatorio";
        } else {
            if( strlen($password ) < 27 )
            {
                $iban_err_min_27_caratteri .= "L' IBAN deve contenere almeno 27 caratteri";

            }

        }

        if ($password != $confermaPassword)
            $due_psw_non_coincidono = "Le due password non coincidono!";



        $CONTROLLA = mysqli_query($connessione,"SELECT email FROM collaboratori WHERE email='$email'");
        $SE_IL_RISULTATO_IMMESSO=mysqli_num_rows($CONTROLLA);

        if($SE_IL_RISULTATO_IMMESSO==0)
        {
            $hash = password_hash($password, PASSWORD_BCRYPT);
            $connessione->query("INSERT INTO collaboratori 
                        (
                            nome,
                            cognome,
                            email,
                            password,
                            citta,
                            indirizzo,
                            indirizzo_txt,
                            civico,
                            cap,
                            telefono,
                            codice_fiscale,
                            iban,
                            categoria_richiesta
                        ) 

                        VALUES 
                        (
                            '$nome', 
                            '$cognome',
                            '$email', 
                            '$hash',
                            '$citta',
                            '$indirizzo',
                            '$indirizzo_txt',
                            '$civico',
                            '$cap',
                            '$telefono',
                            '$codice_fiscale',
                            '$iban'
                        )");

            header("location: index.php?col_reg=Y");

        }

        else
        {
            $indirizzo_email_gia_registrato = 'Indirizzo email, già registrato' ;
        }
    }

for example the field 'nome' and 'cognome', the controls seem non-existent I can't understand why they fail every time,thanks in advance for your support.

  • 1
    Because you don't check if any of variables with errors are __not empty__ – u_mulder Dec 29 '19 at 09:53
  • Use prepared statements to prevent SQL injection, see https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Progman Dec 29 '19 at 15:00
  • The password regex is not correct and shouldn't be used in this form. Besides the delimiter missing, currently it enforces that the password must begin with the letter `A` and ends with the letter `z`. The backslashes, which were originally in the regex you have copied, were there for a reason. Also, limiting the password to 20 makes no sense, why do you want to limit the user password in that way? – Progman Dec 29 '19 at 15:05

1 Answers1

0

The problem is that you set your error variables, but do not check them before you send the query. You have to write some if() statement before sending the SQL query checking if there were any errors before. One way to do this is to use an array and fill it with error messages:

$errors = array();
if (/*name is invalid */) {
    $errors[] = 'Your name is invalid.';
}
if (/*city is invalid */) {
    $errors[] = 'Your city is invalid.';
}
if (/*email already used */) {
    $errors[] = 'The email address is already being used.';
}
//...

// check if there were any validation errors above.
if (count($errors) > 0) {
    // deal with error messages like showing them with 'echo' and `foreach`
} else {
    // all fine, send the query.
}
Progman
  • 16,827
  • 6
  • 33
  • 48