gpt4 book ai didi

php - 如何插入我的验证信息? (SQL, PHP)

转载 作者:行者123 更新时间:2023-11-29 04:40:31 24 4
gpt4 key购买 nike

我有一个小问题,所以我写了一段 HTML 和 PHP 来注册一个帐户。现在我已经完成了验证,但是当所有验证都正确时,我该如何将我的数据插入我的数据库:所以没有错误。

我知道 SQL 语法,不用担心。我只想在所有验证都很好并且没有验证错误时插入。

代码:

<?php
$usernameErr = $emailErr = $passwordErr = $password_valErr = "";
$username = $email = $password = $password_val = "";

if($_SERVER['REQUEST_METHOD'] == 'POST') {
if(empty($_POST['username'])) {
$usernameErr = "Name is required";
} else {
$username = validate_input($_POST['username']);
if($username <= 6) {
$usernameErr = "Username must be longer as 6 characters.";
}
if(!preg_match("/^[a-zA-Z ]*$/", $username)) {
$usernameErr = "Only letters and white space allowed.";
}
}

if(empty($_POST['email'])) {
$emailErr = "Email is required";
} else {
$email = validate_input($_POST['email']);
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format.";
}
}

if(empty($_POST['password'])) {
$passwordErr = "Password is required";
} else {
$password = validate_input($_POST['password']);
if($password <= 8) {
$passwordErr = "Password must be longer as 8 characters.";
}
if(!preg_match("#[0-9]+#", $password)) {
$passwordErr = "Password must contain atleast 1 number.";
}
}

if(empty($_POST['password_val'])) {
$password_valErr = "Password_val is required";
} else {
$password_val = validate_input($_POST['password_val']);
if($password_val != $password) {
$password_valErr = "Password_val must be equal to password.";
}
}
}
?>

<table border="1">
<tr>
<td><label>Username</label><?=' <b>' . $usernameErr . '</b>';?></td>
<td><input type="text" name="username" value="<?=$username;?>" placeholder="Enter your desired username..." /></td>
</tr>
<tr>
<td><label>E-mail</label><?=' <b>' . $emailErr . '</b>';?></td>
<td><input type="text" name="email" value="<?=$email;?>" placeholder="Enter your email address..." /></td>
</tr>
<tr>
<td><label>Password<?=' <b>' . $passwordErr . '</b>';?></label></td>
<td><input type="password" name="password" placeholder="Enter your desired password..." /></td>
</tr>
<tr>
<td><label>Repeat Password<?=' <b>' . $password_valErr . '</b>';?></label></td>
<td><input type="password" name="password_val" placeholder="Repeat your chosen password.." /></td>
</tr>
<tr>
<td><input type="submit" name="register" value="Register" /></td>
</tr>
</table>
</form>

最佳答案

试试这个:

if(empty($_POST['username'])) {
$usernameErr="Name is required";
} else {
$username=validate_input($_POST['username']);
if($username<=6) {
$usernameErr="Username must be longer as 6 characters.";
}
if(!preg_match("/^[a-zA-Z ]*$/",$username)) {
$usernameErr="Only letters and white space allowed.";
}
}
if(empty($_POST['email'])) {
$emailErr="Email is required";
} else {
$email=validate_input($_POST['email']);
if(!filter_var($email,FILTER_VALIDATE_EMAIL)) {
$emailErr="Invalid email format.";
}
}
if(empty($_POST['password'])) {
$passwordErr="Password is required";
} else {
$password=validate_input($_POST['password']);
if($password<=8) {
$passwordErr="Password must be longer as 8 characters.";
}
if(!preg_match("#[0-9]+#",$password)) {
$passwordErr="Password must contain atleast 1 number.";
}
}
if(empty($_POST['password_val'])) {
$password_valErr="Password_val is required";
} else {
$password_val=validate_input($_POST['password_val']);
if($password_val!=$password) {
$password_valErr="Password_val must be equal to password.";
}
}
if(!isset($usernameErr,$usernameErr,$emailErr,$passwordErr,$password_valErr)) {
//Insert QUERY
}

关于php - 如何插入我的验证信息? (SQL, PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29956853/

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