gpt4 book ai didi

php - 提交表单后显示错误信息

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:23 26 4
gpt4 key购买 nike

我有一个注册表格,我在其中检查了电子邮件 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);
}
?>

最佳答案

如果所有代码都在单个文件中,那么您将 PHP 代码移到 html 代码之上,为什么错误没有显示 html 在执行 PHP 代码之前首先呈现?即:

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

关于php - 提交表单后显示错误信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26880569/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com