-2

I have a form and fields are Name, Email, Password. Client-side validation is working perfectly. I am working on server-side validation.

I am getting the issue on else part. For testing, I added echo $email and I am getting the email id. Now that email id will check in the database is exists or not. If exist the display the error if not existing the display the not exist.

if(condition){}
elseif(condition){}
elseif(condition){}
elseif(condition){}
else{
    echo $email; // here I am able to display the email id

    $sql_check_email="SELECT email FROM register WHERE email =?";
    $stmt = $conn->prepare($sql_check_email);
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $stmt->bind_result($email);
    $rows = $stmt->fetch();
    $total_rows = count($rows);
    if( $total_rows > 0 ){
        $_SESSION['email_error']= 'This email is alredy register with us';
        header('location:register');
    }else{
        echo $email;// why my email id not displaying here?
        echo $name;
        echo $password;
        echo $date_of_added;
        echo"Not exist";
    }
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
user9437856
  • 2,360
  • 2
  • 33
  • 92

1 Answers1

-1

Your issue is:

$stmt->bind_result($email);

That line replaces variable with query result.
Just use another variable for results.

You also use fetch result wrong, it returns just true/false/null value of fetch state. Use bind_result binded variable to get your results.

vp_arth
  • 14,461
  • 4
  • 37
  • 66
  • 1
    In the else .... there is no email set as obviously it was not found on the database – RiggsFolly May 03 '18 at 18:09
  • @RiggsFolly, doesn't `bind_param` that work? – vp_arth May 03 '18 at 18:11
  • 1
    Yes, that's right. I tried $stmt->bind_result($email_1); and I got my email in else part. @vp_arth, Can you assist me what's wrong in the fetch result? – user9437856 May 03 '18 at 18:13
  • `$stmt->bind_result($email); $rows = $stmt->fetch();` you bind and fetch the result of your query. If no matching row is found you are binding NOTHING to `$email` – RiggsFolly May 03 '18 at 18:14
  • See [doc](http://php.net/manual/en/mysqli-stmt.fetch.php). You can use returned value as a flag there was data, but not as fetched dataset. Fetched dataset will be in the `bind_result` variables. – vp_arth May 03 '18 at 18:15
  • Thanks for the information @vp_arth, and RiggsFolly. Upvote from my side to notice the error. – user9437856 May 03 '18 at 18:22