在mysql数据库中找不到值时显示消息(displaying a message when value is not found in mysql database)

编程入门 行业动态 更新时间:2024-10-16 18:37:03
在mysql数据库中找不到值时显示消息(displaying a message when value is not found in mysql database)

我试图显示一条消息,如果我的数据库没有该值。 我的代码是:

$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;}

现在,从一开始,mysqli_num_rows($ result)中的值为零。 从页面加载时显示错误。 我希望它只在通过数据库检查用户在字段中给出的值并且不存在时才出现。

请帮忙。 谢谢。

完整代码更新:

<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>

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>

最满意答案

在这个代码基本的html和php我用过。 你必须在开头检查已经设置了$_POST['email']表单。

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>

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>

更多推荐

本文发布于:2023-08-05 22:11:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1440926.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:找不到   数据库中   消息   mysql   message

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!