0

I am trying to display a message if my database doesn't has that value. my code is:

 $dbc = mysqli_connect($hn,$un,$pd,$db);
 $query= "SELECT * FROM sign_up WHERE email = '{$email}'";
 $result = mysqli_query($dbc,$query);
 mysqli_close($dbc);
 $row = mysqli_num_rows($result);

 if($row==0){
 $emailNoExistErr = 'Value not found.';
 echo $emailNoExistErr;}

Now, Since from the beginning the value in mysqli_num_rows($result) is zero. The error is displayed from the time page loads. I want it to appear only when the value given by user in field is checked through database and it is not present.

please help. Thanks.

Full code update:

<body>
    <?php
        if($_SERVER["REQUEST_METHOD"] == "POST"){
                $email = $_POST['email'];
            $password = $_POST['password'];
        }

        $dbc = mysqli_connect($hn,$un,$pd,$db);
        $query= "SELECT * FROM sign_up WHERE email = '{$email}'";
        $result_mail = mysqli_query($dbc,$query);
        mysqli_close($dbc);
        $row = mysqli_num_rows($result_mail);

    ?>

    <div class="cover-container wrap">

        <div id="signup-bg">
            <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
                <ul class="form-style-1">
                    <li>
                        <label for="email">Email</label>
                        <input placeholder="Email*" type="email" name="email" id="email" required>
                    </li>
                    <span class="error"><?php
                        if($row==0){
                            $emailNoExistErr = 'This email is not signed up with us.';
                            echo $emailNoExistErr;}
                        ?></span>
                    <li>
                        <label for="password">Password</label>
                        <input placeholder="Password*" type="password" name="password" id="password" onpaste="return false;" required>
                    </li>
                     <li>
                         <input type="submit" value="Sign in" name="sign_in" id="sign_in">
                    </li>
                </ul>
            </form>

        </div>
        </div>
   </body>
10
  • why are you closing connection ? Commented Feb 19, 2017 at 9:13
  • Because it's a good practice. i read in a book that even though the connection closes itself but at one time only few connections can be made possible and if you have too many connections open they wont function properly. I am sure my problem is not related with that. Commented Feb 19, 2017 at 9:16
  • use it in last when your all work have done. if you want when user enter the value than checked so try isset($email) Commented Feb 19, 2017 at 9:23
  • How are you calling this function? Do you have some JS to share with us? Commented Feb 19, 2017 at 9:24
  • how are you getting value of $email Commented Feb 19, 2017 at 9:42

1 Answer 1

1

In this code basic html & php i used . you have to just check in the beginning that form have $_POST['email'] already set or not.

        if($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['email'])){  /*this for when page load see it's have form's request or not  */
                $email = $_POST['email'];
                $password = $_POST['password'];
                $dbc = mysqli_connect($hn,$un,$pd,$db);
                $query= "SELECT * FROM sign_up WHERE email = '{$email}'";
                $result_mail = mysqli_query($dbc,$query);
                mysqli_close($dbc);
                $row = mysqli_num_rows($result_mail);
        } else {/*this is for first time because we have to set row variable without it , error will show  */
            $row = "" ;
        }


  ?>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

<label for="email">Email</label>
<input placeholder="Email*" type="email" name="email" id="email" required>

<span>
<?php
            if($row===0){
             echo 'This email is not signed up with us.';
            }  
?>

</span>

<label for="password">Password</label>
<input placeholder="Password*" type="password" name="password" required>                    
<input type="submit" ">                 
</form>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.