-3

I want to run two queries at a time in a function to verify the username and email separately when registering. If one of them already exists in the database, it will return the correct error message on the form. I investigate them separately so that they can be linked to two separate messages based on a query. If the username already exists in the database, display the corresponding message. If I put them in a single query, then the separate investigation cannot be done.

My error is: It does not allow you to run two queries at the same time and throws the following error: there is a problem with the preceding parameter. Or it returns an incorrect value.

function pl($connection) {
    $query = "SELECT username FROM users WHERE username = ?";
    $query2 = "SELECT email FROM users WHERE email = ?";

    if ($statment = mysqli_prepare($connection, $query) && $statment2 = mysqli_prepare($connection, $query2)) {
        mysqli_stmt_bind_param($statment, "s", $_POST['usern']);
        mysqli_stmt_execute($statment);
        $result = mysqli_stmt_get_result($statment);
        $record = mysqli_fetch_assoc($result);

        mysqli_stmt_bind_param($statment2, "s", $_POST['email']);
        mysqli_stmt_execute($statment2);
        $result2 = mysqli_stmt_get_result($statment2);
        $record2 = mysqli_fetch_assoc($result2);
    }
    if ($result != null) {
        echo "succes";
        //it will enter even if there is an error
    }
}

How it could be solved to execute two mysqli_prepare() at a time?

Dharman
  • 26,923
  • 21
  • 73
  • 125
  • "it will enter even if there is an error" is rather a dangerous misconception. When there is an error, the code doesт't have to enter anywhere – Your Common Sense Mar 15 '20 at 11:55

1 Answers1

1

Why you do not use one query? Something like:

$query = "SELECT username, email FROM users WHERE username = ? and email = ?";
$statment = mysqli_prepare($connection, $query);
mysqli_stmt_bind_param($statment, "ss", $_POST['usern'], $_POST['email']);
mysqli_stmt_execute($statment);
$result = mysqli_stmt_get_result($statment);
$record = mysqli_fetch_assoc($result);

if (!$record) {
    echo "succes";
    //it will enter even if there is an error
}

also you miss the } at end of your first if

Dharman
  • 26,923
  • 21
  • 73
  • 125
Jens
  • 63,364
  • 15
  • 92
  • 104