how do i resolve this error in php - Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result. i'm trying to create login and sign-up pages for my project, the form is handled by one php file auth-controller, this is the code below;
<?php
use PHPMailer\PHPMailer\PHPMailer;
session_start();
$username = "";
$email = "";
$errors = [];
$conn = new mysqli('localhost', 'id14612068_topadmin', 'Ju<|2<!PUsS{#PE', 'id14612068_topguild');
// SIGN UP USER
if (isset($_POST['signup-btn'])) {
if (empty($_POST['username'])) {
$errors['username'] = 'Username required';
}
if (empty($_POST['email'])) {
$errors['email'] = 'Email required';
}
if (empty($_POST['password'])) {
$errors['password'] = 'Password required';
}
if (isset($_POST['password']) && $_POST['password'] !== $_POST['passwordConf']) {
$errors['passwordConf'] = 'The two passwords do not match';
}
$username = $_POST['username'];
$email = $_POST['email'];
$token = bin2hex(random_bytes(50)); // generate unique token
$password = password_hash($_POST['password'], PASSWORD_DEFAULT); //encrypt password
// Check if email already exists
$sql = "SELECT * FROM users WHERE email='$email' LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$errors['email'] = "Email already exists";
}
if (count($errors) === 0) {
$emailtoken = "poiuztrewqasdfghjklmnbvcxy1234567890";
$emailtoken = str_shuffle($emailtoken);
$emailtoken = substr($emailtoken, 0, 10);
$query = "INSERT INTO users SET username=?, email=?, token=?, password=?, emailToken=?";
$stmt = $conn->prepare($query);
$stmt->bind_param('sssss', $username, $email, $token, $password, $emailtoken);
$result = $stmt->execute();
require_once "PHPMailer/PHPMailer.php";
require_once "PHPMailer/Exception.php";
$mail = new PHPMailer();
$mail->addAddress($email);
$mail->setFrom("mayjriffs97@gmail.com", "mayor1997j");
$mail->Subject = "Reset Password";
$mail->isHTML(true);
$mail->Body = "
Hi,<br><br>
please click on the link below to verify your email:<br>
<a href='localhost/Confirm-Email.php?email=$email&token=$emailtoken'>click here</a><br><br>
Kind Regards,<br>
My Name
";
if (!$mail->send()) {
echo "ERROR: " . $mail->ErrorInfo;
} else {
echo "<h3 class='text-capitalise text-primary'>Please Check your Email Inbox for the link to reset your Password</h3>";
}
if ($result) {
$user_id = $stmt->insert_id;
$stmt->close();
// TO DO: send verification email to user
//sendVerificationEmail($email, $token);
$_SESSION['id'] = $user_id;
$_SESSION['username'] = $username;
$_SESSION['email'] = $email;
$_SESSION['verified'] = false;
$_SESSION['message'] = 'You are registered, please confirm your email address to login!';
$_SESSION['type'] = 'alert-success';
header('location: login.php');
} else {
$_SESSION['error_msg'] = "Database error: Could not register user";
}
}
}
// LOGIN
if (isset($_POST['login-btn'])) {
if (empty($_POST['username'])) {
$errors['username'] = 'Username or email required';
}
if (empty($_POST['password'])) {
$errors['password'] = 'Password required';
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username='$username' LIMIT 1";
$result = mysqli_query($conn, $sql);
$emailcheck = mysqli_fetch_assoc($result);
mysqli_free_result($result);
if($emailcheck['isEmailConfirmed'] == 1){
if (count($errors) === 0) {
$query = "SELECT * FROM users WHERE username=? OR email=? LIMIT 1";
$stmt = $conn->prepare($query);
$stmt->bind_param('ss', $username, $password);
if ($stmt->execute()) {
$result = $stmt->get_result();
$user = $result->fetch_assoc();
if (password_verify($password, $user['password'])) { // if password matches
$stmt->close();
$_SESSION['id'] = $user['id'];
$_SESSION['username'] = $user['username'];
$_SESSION['email'] = $user['email'];
$_SESSION['verified'] = $user['verified'];
$_SESSION['message'] = 'You are logged in!';
$_SESSION['type'] = 'alert-success';
header('location: app/index.php');
exit(0);
} else { // if password does not match
$errors['login_fail'] = "Wrong username / password";
}
} else {
$_SESSION['message'] = "Database error. Login failed!";
$_SESSION['type'] = "alert-danger";
}
}
}else{
$errors['Confirm_Email_Failed'] = "You have not confirmed your email <br> Please confirm your email to login";
}
}
everything seems to be working perfectly on the local server, but when i try to sign-up or login into the project on the live server, i get an error on this line -
$sql = "SELECT * FROM users WHERE email='$email' LIMIT 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
$errors['email'] = "Email already exists";
}
how can i fix this please
Aucun commentaire:
Enregistrer un commentaire