gpt4 book ai didi

php - 数据直接存储到mysql数据库,无需使用php设置任何验证

转载 作者:行者123 更新时间:2023-11-29 07:46:50 25 4
gpt4 key购买 nike

我是php和mysql的新手。目前我正在开发一个使用 php 和 mysql 来存储数据的注册系统。所以我已经连接了数据库,并且正在对用户填写的表单信息进行一些验证。我决定不使用 html“必需”功能。但验证根本不起作用。虽然信息填写为空,但没有显示任何错误,但仍然可以继续下一步,将信息插入数据库。任何帮助将不胜感激,看看哪里出了问题......

这是我的代码。

<div id="content">
<form action="signup.php"method="POST">
<fieldset>
<legend>Sign up your Watevershit account to unlock more shit!</legend>


<p>
<label>
<span>Username :</span>
<input type="text"name="username">
</label>
</p>

<p><?php if(isset($errors['username1']) echo $errors['username1'])?></p>


<p>
<label>
<span>Password</span>
<input type="password"name="password">
</label>
</p>
<p><?php if(isset($errors['password1']) echo $errors['password1'])?></p>

<p>
<label>
<span>Confirm Password :</span><input type="password"name="password">
</label>
</p>

<p>
<label>
<span>Email:</span>
<input type="email"name="email">
</label>
</p>
<p><?php if(isset($errors['email1']) echo $errors['email1'])?></p>

<p>
<label>
<input type="submit"id="submit"value="Sign Up Now!">
</label>
</p>

<p>
<label>
<span><a href="login.html">Already member?Log in here</a></span>
</label>
</p>
</fieldset>

</form>

</div>

这是我的 php 脚本,我已经完成了所有验证,但它不起作用并将数据插入数据库,尽管表单没有填写任何内容。那么,这里出了什么问题?

<?php
include ('config.php');

//declare variables
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];

//define error variables
$usernameERR = $emailERR =$passwordERR="";
$username=$email=$password="";

//validation

if ($_SERVER["REQUEST_METHOD"] == "POST"){
//not empty
//at least 3 characters long


//start the validation

//check the username
if(empty($_POST['username'])){
$errors['username1'] = "Required fields"
}

if (strlen($username) <3 ) {
$errors['username2'] ="Username must at least 3 characrters long.";
}

//check the password
if (empty($_POST['password'])){
$errors['password1'] ="Required fields";
}

if (strlen($password) < 8) {
$errors['password2'] = "Password must be 8 characrters long";
}


//check the email
if (empty($_POST['email'])){
$errors['email1'] = "Required fields";
}

if (strlen($email) < 12){
$errors['email2'] ="Email must at least 12 characrters long";
}

//check the errors
if(count($errors) == 0){
//redirect to sucess page
header('Location:login.html');
}
}


// if all correct,insert data to the database

$query="INSERT INTO user(Username,Password,Email) VALUES ('".$_POST['username']."','".$_POST['password']."','".$_POST['email']."')";
mysqli_query($con,$query);


?>

有什么想法吗?

最佳答案

试试这个..

    <?php        
$errors=array();
if ($_SERVER["REQUEST_METHOD"] == "POST"){
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
//not empty
//at least 3 characters long


//start the validation

//check the username
if(empty($_POST['username'])){
$errors['username1'] = "Required fields";
}

if (strlen($username) <3 ) {
$errors['username2'] ="Username must at least 3 characrters long.";
}

//check the password
if (empty($_POST['password'])){
$errors['password1'] ="Required fields";
}

if (strlen($password) < 8) {
$errors['password2'] = "Password must be 8 characrters long";
}


//check the email
if (empty($_POST['email'])){
$errors['email1'] = "Required fields";
}

if (strlen($email) < 12){
$errors['email2'] ="Email must at least 12 characrters long";
}

//check the errors
if(count($errors) == 0){
$query="INSERT INTO user(Username,Password,Email) VALUES ('".$_POST['username']."','".$_POST['password']."','".$_POST['email']."')";
mysqli_query($con,$query);

}
}
?>
<div id="content">
<form action="" method="POST">
<fieldset>
<legend>Sign up your Watevershit account to unlock more shit!</legend>


<p>
<label>
<span>Username :</span>
<input type="text"name="username">
</label>
</p>

<p><?php if(!empty($errors['username1'])) { echo $errors['username1']; } ?></p>


<p>
<label>
<span>Password</span>
<input type="password" name="password">
</label>
</p>
<p><?php if(!empty($errors['password1'])) { echo $errors['password1']; }?></p>

<p>
<label>
<span>Confirm Password :</span><input type="password"name="password">
</label>
</p>

<p>
<label>
<span>Email:</span>
<input type="email"name="email">
</label>
</p>
<p><?php if(!empty($errors['email1'])) { echo $errors['email1']; }?></p>

<p>
<label>
<input type="submit"id="submit"value="Sign Up Now!">
</label>
</p>

<p>
<label>
<span><a href="login.html">Already member?Log in here</a></span>
</label>
</p>
</fieldset>

</form>

</div>

关于php - 数据直接存储到mysql数据库,无需使用php设置任何验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519696/

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