Hej,
Dorwałem darmowy szablon z formularzem logowania. Wszystko fajnie działa ale potrzebuje też żeby użytkownik przy rejestracji podawał email. No i własnie przy przerobieniu kodu (dodaniu inputa z emailem i dopisaniu kodu php) zwraca mi komunikat:
Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in /uploads/register.php on line 86
Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in /uploads/register.php on line 146
Będę wdzięczny za pomoc
To co dopisałem
// Validate email
$email_err = "Proszę podać email.";
} else{
// Prepare a select statement
$sql = "SELECT id FROM users WHERE email = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_email);
// Set parameters
$param_email = trim($_POST["email"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
/* store result */
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
$email_err = "Ten email już był wykorzystany.";
} else{
$email = trim($_POST["email"]); }
} else{
echo "Oops! Coś poszło nie tak spróbuj później."; }
}
// Close statement
mysqli_stmt_close($stmt);
}
<div class="form-group <?php echo (!empty($email_err)) ? 'has-error' : ''; ?>">
<input type="text" name="email" class="form-control" value="<?php echo $email; ?>">
<span class="help-block"><?php echo $email_err; ?></span>
Cały kod :
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$username = $password = $email = $confirm_password = "";
$username_err = $password_err = $email_err = $confirm_password_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate username
$username_err = "Please enter a username.";
} else{
// Prepare a select statement
$sql = "SELECT id FROM users WHERE username = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_username);
// Set parameters
$param_username = trim($_POST["username"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
/* store result */
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
$username_err = "This username is already taken.";
} else{
$username = trim($_POST["username"]); }
} else{
echo "Oops! Something went wrong. Please try again later."; }
}
// Close statement
mysqli_stmt_close($stmt);
}
///////////////////// DODANE PHP ///////////////////////////
// Validate email
$email_err = "Proszę podać email.";
} else{
// Prepare a select statement
$sql = "SELECT id FROM users WHERE email = ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_email);
// Set parameters
$param_email = trim($_POST["email"]);
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
/* store result */
mysqli_stmt_store_result($stmt);
if(mysqli_stmt_num_rows($stmt) == 1){
$email_err = "Ten email już był wykorzystany.";
} else{
$email = trim($_POST["email"]); }
} else{
echo "Oops! Coś poszło nie tak spróbuj później."; }
}
// Close statement
mysqli_stmt_close($stmt);
}
///////////////////// DODANE PHP KONIEC///////////////////////////
// Validate password
$password_err = "Podaj hasło.";
$password_err = "Hasło musi mieć co najmniej 6 znaków.";
} else{
$password = trim($_POST["password"]); }
// Validate confirm password
$confirm_password_err = "Potwierdź hasło.";
} else{
$confirm_password = trim($_POST["confirm_password"]); if(empty($password_err) && ($password != $confirm_password)){ $confirm_password_err = "Hasło nie pasuje.";
}
}
// Check input errors before inserting in database
if(empty($username_err) && empty($email_err) && empty($password_err) && empty($confirm_password_err)){
// Prepare an insert statement
$sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password, $param_email);
// Set parameters
$param_username = $username;
$param_email = $email;
$param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
// Redirect to login page
echo("<script>location.href = '/login.php?msg=$msg';</script>"); } else{
echo "Something went wrong. Please try again later."; }
}
// Close statement
mysqli_stmt_close($stmt);
}
// Close connection
mysqli_close($link);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sign Up</title>
<link rel="stylesheet" type="text/css" href="css/mystylenew2.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
body{ font-family: 'Fira Sans', sans-serif; font-size: 14px; }
.wrapper{ width: 350px; padding: 20px; }
</style>
</head>
<body>
<BR><br><br><BR><br><br>
<div class="wrapper"; style = "text-align: center; background-color:WHITE; border-radius: 25px;";>
<h2>Zarejestruj się</h2>
<p>Wypełnij pola aby założyć konto.</p>
<div class="form-group
<?php echo (!empty($username_err)) ?
'has-error' : ''; ?>">
<label>Użytkownik</label>
<input type="text" name="username" class="form-control" value="
<?php echo $username; ?>">
<span class="help-block">
<?php echo $username_err; ?></span>
</div>
<div class="form-group
<?php echo (!empty($password_err)) ?
'has-error' : ''; ?>">
<label>Hasło</label>
<input type="password" name="password" class="form-control" value="
<?php echo $password; ?>">
<span class="help-block">
<?php echo $password_err; ?></span>
</div>
<!-- //////////////////////////// DODANE HTML ////////////////////////////////////////////////////// -->
<div class="form-group
<?php echo (!empty($email_err)) ?
'has-error' : ''; ?>">
<label>Email</label>
<input type="text" name="email" class="form-control" value="
<?php echo $email; ?>">
<span class="help-block">
<?php echo $email_err; ?></span>
</div>
<!-- //////////////////////////// DODANE HTML KONIEC ////////////////////////////////////////////////////// -->
<div class="form-group
<?php echo (!empty($confirm_password_err)) ?
'has-error' : ''; ?>">
<label>Potwierdź hasło</label>
<input type="password" name="confirm_password" class="form-control" value="
<?php echo $confirm_password; ?>">
<span class="help-block">
<?php echo $confirm_password_err; ?></span>
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Wyślij">
<input type="reset" class="btn btn-default" value="Resetuj">
</div>
<p>Masz już konto? <a href="login.php">Zaloguj się tutaj</a>.</p>
</form>
</div>
</body>
</html>