gpt4 book ai didi

javascript - 由于错误的错误处理,表单数据未插入数据库

转载 作者:搜寻专家 更新时间:2023-10-31 21:48:52 25 4
gpt4 key购买 nike

我尝试在我的注册页面中实现表单验证。我给每个错误一个相应的名称,例如emailErr passErr repeatpassErr 默认情况下变量是一个空字符串,否则当我正确提交表单时,html代码将返回一个错误,说有一个 undefined variable 。但是将错误名称定义为空字符串的问题在于,信息永远不会像我使用 if(!isset($emailErr, $passErr, $repeatpassErr)) 那样进入数据库,因为始终设置变量。我不知道是否有其他方式来显示错误消息,或者是否可以通过其他方式修复 if 语句。我希望你可以帮助我!

这是我的 php 代码:

<?php
session_start();

//connect to the database
require 'opendb.php';

$emailErr = $passErr = $repeatpassErr = "";

//check if a variable is set, in this case the register button
if(isset($_POST['register'])) {
//variable declaration
$mail = htmlspecialchars_decode($_POST['mail'], ENT_QUOTES);
$password = $_POST['password'];
$passwordrepeat = $_POST['passwordrepeat'];
$province = $_POST['province'];

//check if email input isn't blank and if provided email is valid
if (empty($mail)) {
$emailErr = "Vul uw e-mailadres in";
} elseif(!filter_var($mail, FILTER_VALIDATE_EMAIL) and !empty($mail)) {
$emailErr = "Vul een geldig e-mailadres in";
} else {
// prepare statement for checking whether an emailaddress is already taken
$stmt = $db->prepare('SELECT mail FROM users WHERE mail = :mail');
//bind the user's mail and fetch the row of the emailaddress that is the same
$stmt->execute(array(':mail' => $mail));
$row = $stmt->fetch(PDO::FETCH_ASSOC);
//check if a row with an equal emailaddress can be found
if(!empty($row['mail'])){
$emailErr = 'Dit e-mailadres is al in gebruik';
}
}

//check if password inputs aren't blank and longer than 5 characters
if (empty($password)) {
$passErr = "Vul uw wachtwoord in";
} elseif(strlen($password) < 5 and !empty($password)) {
$passErr = "Wachtwoord is te kort";
}
if (empty($passwordrepeat)) {
$repeatpassErr = "Vul uw herhaalde wachtwoord in";
} elseif(strlen($passwordrepeat) < 5 and !empty($passwordrepeat)) {
$repeatpassErr = "Herhaalde wachtwoord is te kort";
}

//check if the password is equal to the repeated password
if($password != $passwordrepeat and !empty($password) and !empty($passwordrepeat)) {
$passErr = 'Wachtwoorden komen niet overeen';
}

//if (empty($province)) $error[] = "Vul uw provincie in";


//if no error is found, execute statement
if(!isset($emailErr, $passErr, $repeatpassErr)) {
//hash the password
$hashedpassword = password_hash($password, PASSWORD_DEFAULT);
//prepare statement for the insertion of user's mail, password and province into table
$sql = "INSERT INTO users (mail, password, province) VALUES (:mail, :password, :province)";
$stmt = $db->prepare($sql);
//bind the variables to the placeholder that is used to prepare the statement
$stmt->bindValue(':mail', $mail);
$stmt->bindValue(':password', $hashedpassword);
$stmt->bindValue(':province', $province);
//execute the statement and insert account in database
$result = $stmt->execute();
//check if signup is succesful and create a session
if($result){
$_SESSION['login_user'] = $mail;
header('Location: views.php');
exit;
}
}
}
?>

这是我的 html 代码:

<body>      
<div class="container">
<div class="register">
<form method="post" action="userregistration.php">
<h1>Sign up</h1>
<label for="email">E-mailadres:</label><br>
<div class="infobox">
<i class="fa fa-envelope"></i>
<input class="text-input" type="email" name="mail" placeholder="example:emailname@mail.com"><br>
<span class="error"><?php echo $emailErr;?></span><br>
</div>

<label for="password">Wachtwoord:</label><span class="passrequirements"> (minimaal 5 karakters)</span><br>
<div class="infobox">
<i class="fa fa-key"></i>
<input class="text-input" type="password" name="password" placeholder="******"><br>
<span class="error"><?php echo $passErr;?></span><br>
</div>

<label for="password">Herhaal wachtwoord:</label><br>
<div class="infobox">
<i class="fa fa-key"></i>
<input class="text-input" type="password" name="passwordrepeat" id="password" placeholder="******"><br>
<span class="error"><?php echo $repeatpassErr;?></span><br>
</div>

<label for="country">Provincie:</label><br>
<div class="infobox">
<i class="fa fa-flag"></i>
<select name="province">
<option value="DR">Drenthe</option>
<option value="FL,">Flevoland</option>
<option value="FR">Friesland</option>
<option value="GE">Gelderland</option>
<option value="GR">Groningen</option>
<option value="LI">Limburg</option>
<option value="NB">Noord-Brabant</option>
<option value="NH">Noord-Holland</option>
<option value="OV">Overijssel</option>
<option value="UT">Utrecht</option>
<option value="ZE">Zeeland</option>
<option value="ZH">Zuid-Holland</option>
</select><br>
</div>

<button type="submit" class="signup-button" name="register">Registreer</button><br>
<a class="login-link" href="userlogin.php">Ik heb al een account</a>
</form>
</div>
</div>
</body>

最佳答案

除了使用 isset() 检查是否有错误,您可以使用 empty():

if(empty($emailErr) && empty($passErr) && empty($repeatpassErr)) {

这样,如果错误变量是空字符串,它将通过,但如果任何变量的值不是空字符串,则不会。

关于javascript - 由于错误的错误处理,表单数据未插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48505870/

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