我目前正在研究php和html表单。我想检查是否有重复的用户或电子邮件。下面是我的HTML代码。
<div class="container" align="center">
<form method="post" class="form-horizontal" action="signup.php">
<?php include('errors.php');?>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
<label class="col-sm-4 control-label">Full name</label>
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="First_Name" placeholder="First name" <?php echo $First_Name?> />
</div>
<div class="col-xs-4">
<input type="text" class="form-control" name="Last_Name" placeholder="Last name" <?php echo $Last_Name?> />
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-user"></span>
<label class="control-label">Username</label>
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="Username" <?php echo $Username?>/>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-envelope"></span>
<label class="control-label">Email address</label>
</div>
<div class="col-xs-6">
<input type="text" class="form-control" name="Email" <?php echo $Email?>/>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-lock"></span>
<label class="control-label">Password</label>
</div>
<div class="col-xs-6">
<input type="password" class="form-control" name="Password_1" />
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-lock"></span>
<label class="control-label">Confirm Password</label>
</div>
<div class="col-xs-6">
<input type="password" class="form-control" name="Password_2" />
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-info"></span>
<label class="control-label">Gender</label>
</div>
<div class="col-xs-6">
<div class="radio">
<label>
<input type="radio" name="Gender" value="male" /> Male
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="Gender" value="female" /> Female
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="Gender" value="other" /> Other
</label>
</div>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-date"></span>
<label class="control-label">Date of birth</label>
</div>
<div class="col-xs-6">
<input type="date" class="form-control" name="Birthday" placeholder="YYYY/MM/DD" <?php echo $Birthday?>/>
</div>
</div>
<div class="input-group">
<div class="input-group-addon">
<span class="glyphicon glyphicon-home"></span>
<label class="control-label">Address</label>
</div>
<div class="col-xs-5">
<textarea placeholder="Enter Address Here.." rows="3" name="Address" class="form-control"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-9 col-xs-offset-3">
<button type="submit" class="btn btn-primary" name="reg_user">Submit</button>
</div>
</div>
</form>
</div>
&#13;
这是我的datapost文件
<html>
<body>
<?php
include 'databaseconn.php';
print_r($_POST);
$First_Name = "";
$Last_Name= "";
$Username= "";
$Email= "";
$Password_1="";
$Password_2="";
$Gender= "";
$Birthday= "";
$Address= "";
$errors = array();
if (isset($_POST['reg_user']))
{
$First_Name = mysqli_real_escape_string($connect,$_POST['First_Name']);
$Last_Name= mysqli_real_escape_string($connect,$_POST['Last_Name']);
$Username= mysqli_real_escape_string($connect,$_POST['Username']);
$Email= mysqli_real_escape_string($connect,$_POST['Email']);
$Password_1=mysqli_real_escape_string($connect,$_POST['Password_1']);
$Password_2=mysqli_real_escape_string($connect,$_POST['Password_2']);
$Gender=mysqli_real_escape_string($connect,$_POST['Gender']);
$Birthday=mysqli_real_escape_string($connect,$_POST['Birthday']);
$Address=mysqli_real_escape_string($connect,$_POST['Address']);
if (empty($First_Name))
{
array_push($errors,"First name is required");
}
if (empty($Last_Name))
{
array_push($errors,"Last name is required");
}
if (empty($Username))
{
array_push($errors,"Username is required");
}
if (empty($Email))
{
array_push($errors,"Email id is required");
}
if (empty($Password_1))
{
array_push($errors,"Password is required");
}
if ($Password_1 != $Password_2)
{
array_push($errors,"Two password do not match");
}
if (empty($Gender))
{
array_push($errors,"Gender is required");
}
if (empty($Birthday))
{
array_push($errors,"Birthday is required");
}
if (empty($Address))
{
array_push($errors,"Address is required");
}
}
$user_check_query= "SELECT * FROM `user_data` WHERE Username='$Username' OR Email ='$Email' LIMIT 1";
$result = mysqli_query($connect, $user_check_query);
$user = mysqli_fetch_assoc($result);
if ($user)
{
if($user['Username'] === $Username)
{
array_push($errors, "Username already exists");
}
if($user['Email'] === $Email)
{
array_push($errors, "Email already exists");
}
}
if (count($errors) == 0)
{
$Password = md5($Password_1);
mysqli_query($connect, "INSERT INTO `user_data` (`First_Name`, `Last_Name`, `Username`, `Email`, `Password`, `Gender`, `Birthday`, `Address`) VALUES ('$First_Name', '$Last_Name', '$Username', '$Email', '$Password', '$Gender', '$Birthday', '$Address')");
}
if(mysqli_affected_rows($connect)>0)
{
echo'<p> User successfully registered </p>';
echo'<a href="index.php"> Go BAck </a>';
}
else
{
echo 'Registration not successfull';
echo 'mysqli_error($connect)';
}
?>
</body>
</html>
&#13;
我的数据库连接文件是
<?php
$servername = "localhost";
$username = "root";
$password = "";
$databasename="smartparking";
// Create connection
$connect = mysqli_connect($servername, $username, $password,$databasename);
// Check connection
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
&#13;
当我运行代码并输入数据时,它不会向数据库添加数据。它正在检查用户名和电子邮件。但是没有为新用户插入数据。