我有一份注册表格,其中我检查了电子邮件ID和状态,以便一个人不能再使用同一封电子邮件注册。表单和脚本都在一个页面上,页面是.php格式。问题是虽然检查工作正常,但我无法在电子邮箱前显示错误消息。如果有人能帮助我,我将不胜感激
<form role="form" action="" autocomplete="off" method="POST">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="name" class="form-control" name="name" placeholder="Company Name" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="email" class="form-control" name="email" placeholder="email address" required>
</div>
<span><?php echo $errormsg;?></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" class="form-control" name="password" placeholder="Password" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="tel" id="tel" class="form-control" name="phoneno" pattern="\d{10}" placeholder="Please enter a ten digit phone number" required />
</div>
<span class="help-block"></span>
<input class="form-control" value="" type="hidden" name="status">
<button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Sign Up</button>
</form>
<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$phoneno = mysqli_real_escape_string($con, $_POST['phoneno']);
$status = mysqli_real_escape_string($con, $_POST['status']);
$sql = "SELECT * FROM register WHERE email='".$email."' AND status='1'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
$errormsg="email already exist";
}
else
{
$sql="INSERT INTO register (name,email,password,phoneno,status) VALUES ('$name','$email','$password','$phoneno','1')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
exit;
mysqli_close($con);
}
?>
答案 0 :(得分:2)
如果所有代码都在单个文件中,那么你将PHP代码转移到html代码之上,为什么在执行PHP代码之前错误没有显示html首先呈现?即:
<?php
$con=mysqli_connect("localhost","root","","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$name = mysqli_real_escape_string($con, $_POST['name']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$phoneno = mysqli_real_escape_string($con, $_POST['phoneno']);
$status = mysqli_real_escape_string($con, $_POST['status']);
$sql = "SELECT * FROM register WHERE email='".$email."' AND status='1'";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0)
{
$errormsg="email already exist";
}
else{
$sql="INSERT INTO register (name,email,password,phoneno,status) VALUES ('$name','$email','$password','$phoneno','1')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
exit;
mysqli_close($con);
}
?>
<form role="form" action="" autocomplete="off" method="POST">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="name" class="form-control" name="name" placeholder="Company Name" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="email" class="form-control" name="email" placeholder="email address" required>
</div>
<?php echo $errormsg;?></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" class="form-control" name="password" placeholder="Password" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="tel" id="tel" class="form-control" name="phoneno" pattern="\d{10}" placeholder="Please enter a ten digit phone number" required />
</div>
<span class="help-block"></span>
<input class="form-control" value="" type="hidden" name="status">
<button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Sign Up</button>
</form>
答案 1 :(得分:1)
您应该设置/初始化上面的错误容器,并首先在html上面进行检查。
不要忘记使用准备好的陈述!
<?php
$errormsg = '';
$con = mysqli_connect("localhost","root","","db_name");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$name = $_POST['name'];
$email = $_POST['email'];
$password = $_POST['password'];
$phoneno = $_POST['phoneno'];
$status = $_POST['status'];
$sql = "SELECT * FROM register WHERE email = ? AND status = '1' ";
$check = $con->prepare($sql);
$check->bind_param('s', $email);
$check->execute();
if ($check->num_rows > 0) {
$errormsg = "email already exist";
}
else{
$sql = "INSERT INTO register (name,email,password,phoneno,status) VALUES (?, ?, ?, ?, '1')";
$insert = $con->prepare($sql);
$insert->bind_param('ssss', $name, $email, $password, $phoneno);
$insert->execute();
}
?>
<form role="form" action="" autocomplete="off" method="POST">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="name" class="form-control" name="name" placeholder="Company Name" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="email" class="form-control" name="email" placeholder="email address" required>
</div>
<?php echo $errormsg; ?></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" class="form-control" name="password" placeholder="Password" required>
</div>
<span class="help-block"></span>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="tel" id="tel" class="form-control" name="phoneno" pattern="\d{10}" placeholder="Please enter a ten digit phone number" required />
</div>
<span class="help-block"></span>
<input class="form-control" value="" type="hidden" name="status">
<button class="btn btn-lg btn-primary btn-block" name="submit" type="submit">Sign Up</button>
</form>
答案 2 :(得分:0)
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="email" class="form-control" name="email" placeholder="email address" required>
<br><span><?php echo $errormsg;?></span>
</div>