gpt4 book ai didi

php - 即使数据无效,表单仍然接受数据

转载 作者:行者123 更新时间:2023-11-29 22:08:48 26 4
gpt4 key购买 nike

我的表单仍然显示“注册成功!”并将数据存储在mysql中,即使它不是有效数据并且是空的。然后在显示“注册成功”后显示错误。

它应该运行的方式是,当输入无效或为空时,它不会存储在数据库中,也不会提醒“注册成功!”。我将 php 代码与 html 放在同一个文件中,具体放在 html 代码的顶部。

php

<?php
mysql_connect("localhost","root","");
mysql_select_db("registration");

if (isset($_POST['register'])){
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$email = $_POST['email'];
$pword = $_POST['password_verify'];

if($_POST){
$errors = array();

if(empty($_POST['first_name'])){
$errors['first_name1'] = "This field cannot be empty";
}
if (empty($_POST['last_name'])){
$errors['last_name1'] = "Please enter your name last name.";
}
if (empty($_POST['email'])){
$errors['email1'] = "Please enter email address.";
}
if (empty($_POST['password'])){
$errors['password1'] = "Please enter password.";
}
if (empty($_POST['password_verify'])){
$errors['password_verify1'] = "Please enter password.";

}
if (strlen($_POST['password']) < 6){
$errors['password2'] = 'Your password must be at least 6 characters';
}
if ($_POST['password'] != $_POST['password_verify']){
$errors['password_verify2'] = 'Your passwords do not match.';
}

$check_email = "select * from customer_info where Email='$email'";
$run = mysql_query($check_email);

if (mysql_num_rows($run) >0){
$errors['email2'] = 'Email already exists.';
}
$query = "insert into customer_info (First_Name,Last_Name,Email, Password) values ('$fname', '$lname', '$email', '$pword')";

if(mysql_query($query)){
echo "<script>alert('Registration Successful!')</script>";
}
}
}
?>

最佳答案

您应该在此语句之前添加条件

$query = "insert into customer_info (First_Name,Last_Name,Email, Password) values ('$fname', '$lname', '$email', '$pword')";

应该是:

if(count($errors)>0){
echo "<script>alert('Error!')</script>";
} else {
$query = "insert into customer_info (First_Name,Last_Name,Email, Password) values ('$fname', '$lname', '$email', '$pword')";
if(mysql_query($query)){
echo "<script>alert('Registration Successful!')</script>";
}
}
}

关于php - 即使数据无效,表单仍然接受数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31891610/

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