如果我的数据库没有该值,我试图显示一条消息。 我的代码是:
$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>
答案 0 :(得分:1)
在此代码中基本的html&amp; 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>