PHP form not loading password match
What you're supposed to do to keep it efficient is to check if the passwords match first or not, then to check the existence of the email address, and from that point do your logic, so by that your code should look something like this:
if (isset($_POST['password']) && isset($_POST['password1'])) {
    $password = $_POST['password'];
    $password1 = $_POST['password1'];

    if ($password === $password1) {
        $query = "SELECT email ....";
        $result = $connessione->query($query);

        if ($result->num_rows > 0) {
            echo "L'email ....";
        } else {
            $sql = "INSERT ....";
            if ($connessione->query($sql) === true) {
                ....
            }
        }
    }
}

other than that, important notes:

$password = $_POST['password']; overwrites your $password = $connessione->real_escape_string($_POST['password']);, name your variables wisely.
I highly recommend using PHP PDO to handle databases when you're working with databases, or MySQLi if you know what you're doing.
Revise your execution plan so you don't to run queries that might not be used because of a false condition.


I'm trying to build a Sign In / Sign Up web server with PHP, which fills a mysql table.
The code is running kind of well, but when I tried to implement a control for the password to be the same, it seems like it skips the password match code and goes directly to the login form.
Here's the code:
<?php

require_once('config.php');

$email = $connessione->real_escape_string($_POST['email']);
$username = $connessione->real_escape_string($_POST['username']);
$password = $connessione->real_escape_string($_POST['password']);
$hashed_password = password_hash($password, PASSWORD_DEFAULT);


// Controlla se l'email esiste già nel database
$query = "SELECT * FROM utenti WHERE email = '$email'";
$result = $connessione->query($query);

if ($result->num_rows > 0) {
    echo "L'email esiste già nel database. Si prega di utilizzare un'altra email.";
} else {
    // L'email non esiste nel database, procedi con la creazione dell'account


$passwordMatchError = '';

if(isset($_POST['password']) && isset($_POST['password1'])) {
    $password = $_POST['password'];
    $password1 = $_POST['password1'];

    if($password === $password1) {

        header("Location: login.html");
        exit();
      
    } else {
        $passwordMatchError = 'Le password non corrispondono.'; 
    }
}


    $sql = "INSERT INTO utenti (email, username, password) VALUES ('$email', '$username', '$hashed_password')";
    if ($connessione->query($sql) === true) {
        // Registrazione avvenuta con successo, reindirizza l'utente alla pagina desiderata
        header("Location: login.html");
        exit();
    } else {
        echo "Errore durante la registrazione utente: " . $connessione->error;
    }
}

$connessione->close();

?>


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <div>
        <form action="register.php" method="POST">
            <h2>Registrati</h2>
            <label for="email">Email</label>
            <input type="text" name="email" id="email" placeholder="Inserisci la mail" required>
           
            <label for="username">Username</label>
            <input type="text" name="username" id="username" placeholder="Inserisci username" required>
            
            <label for="password">Password</label>
            <input type="password" name="password" id="password" placeholder="Inserisci la password" required>
    
            <label for="password1">Conferma Password</label>
            <input type="password" name="password1" id="password1" placeholder="Conferma password" required>
    
            <input type="submit" value="Invia">
    
            <?php if (isset($error)) : ?>
                <p><?php echo $error; ?></p>
            <?php endif; ?>
    
            <p>Hai già un account? <a href="login.html">Accedi</a></p>
        </form>
    </div>
</body>
</html>

So far I tried to implement the register.php directly to the "index.php", but nothing worked. I was expecting the echo to show up when the user submits the form with two non-matching passwords, but when i click on submit it brings me directly to the login page, skipping the error. I think it brings the user directly to the login despite the password aren't matching or maybe it's skipping the pass control.
