Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this PHP code, which registers a user, all form validation is done by jQuery and request is sent by ajax to this PHP page. Everything is working fine except its not checking if email and mobile is already registered.


PHP
<pre><?php
require('db_connection.php');
$error= "";
if($_SERVER['REQUEST_METHOD']=="POST"){
    $name=$_POST['name'];
    $email=$_POST['email'];
    $mobile=$_POST['mobile'];
    $password=$_POST['password'];
    $hashed_password = password_hash($_POST['password'], PASSWORD_DEFAULT);
    $sql = "SELECT `user_email`,`user_mobile` FROM `users` where `user_email` = ? OR `user_mobile` = ? LIMIT 1";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("ss",$email,$mobile);
    $stmt->execute();
    $stmt->store_result();
    if($stmt->num_rows() > 0){
        $stmt->fetch();
        if($email==isset($row['user_email'])){
            echo "email exists";
        }
        if($mobile==isset($row['user_mobile'])){
            echo "mobile exists";
        }
    }else{
        $stmt= $conn->prepare("INSERT INTO `users`(`user_name`, `user_email`, `user_mobile`, `user_password`) values (?, ?, ?, ?)");
        $stmt->bind_param("ssss",$name,$email,$mobile,$hashed_password);
        $stmt->execute();
        echo "successfull";  
    }
    $stmt->close();  
}
 $conn->close();
?>


What I have tried:

from my observation the problem is somewhere I'm fetching the rows, because the registration is happening with different email and mobile.
Posted
Updated 2-May-22 9:44am

1 solution

PHP
$stmt->fetch();

You are not capturing the returned row, so you have nothing to compare. See PHP: PDOStatement::fetch - Manual[^] for correct usage.
 
Share this answer
 
Comments
Sameer Sharan 3-May-22 1:07am    
Hi can you please help to fix the code , I tried the document but the thing is I'm not using PDO its just prepared statement and I cannot pass any arguments in fetch()
Richard MacCutchan 3-May-22 3:17am    
You need to reference returned values as described at PHP: mysqli_stmt::fetch - Manual[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900