<html>
<head></head>
<body>
<form action="register.php" method="post">
Username: <input type="text" name="uname" >
Password: <input type="text" name="upass" >
Email: <input type="text" name="uemail" >
<input type="submit" value="register" name="register" >
</form>
</body>
</html>
<?php
$con= mysqli_connect("localhost","root","","users") or die('Error Connecting to Database');
if(isset($_POST['register']))
{
$user_name=$_POST['uname'];
$user_pass=$_POST['upass'];
$user_email=$_POST['uemail'];
if($user_name=='')
{
echo"<script>alert('Please enter the name')</script>";
exit();
}
if($user_pass=='')
{
echo"<script>alert('Please enter the password')</script>";
exit();
}
if($user_email=='')
{
echo"<script>alert('Please enter the email')</script>";
exit();
}
$check_email="select * from accounts WHERE email='$user_email' and username='$user_name'";
$result=mysqli_query($con,$check_email);
$count=mysqli_num_rows($result) or die('Connection error');//here is the cause of the error
if($count>0)
{
echo "<script>alert('User already exists , Please try another one!')</script>";
exit();
}
$insert_user="insert into accounts (username,password,email) VALUE ('$user_name','$user_pass','$user_email')";
if(mysqli_query($con,$insert_user))
{
echo"<script>window.open('welcome.php','_self')</script>";
}
}
?>
答案 0 :(得分:0)
按照守则观察
1)您正在尝试为数据库中不存在其帐户的新用户获取记录。因此,如果您输入新的用户名,电子邮件,密码它将自动进入
die('Connection error');
因为这里的数字将为“0”。因此PHP脚本执行停止,记录不会插入DB。
试试以下代码。 !!
<?php
$con= mysqli_connect("localhost","root","root","testdb") or die('Error Connecting to Database');
if(isset($_POST['register']))
{
$user_name=$_POST['uname'];
$user_pass=$_POST['upass'];
$user_email=$_POST['uemail'];
if(empty($user_name))
{
echo"<script>alert('Please enter the name')</script>";
exit();
}
if(empty($user_pass))
{
echo"<script>alert('Please enter the password')</script>";
exit();
}
if(empty($user_email))
{
echo"<script>alert('Please enter the email')</script>";
exit();
}
$check_email="select * from accounts WHERE email='$user_email' OR name='$user_name'";
$result = mysqli_query($con,$check_email);
while($row = $result->fetch_array()){
if($user_email == $row['email'] || $user_name == $row['name']){
echo "<script>alert('User already exists , Please try another one!')</script>";
exit();
}
}
$insert_user="insert into accounts (name,password,email) VALUE ('$user_name','$user_pass','$user_email')";
if(mysqli_query($con,$insert_user))
{
echo"<script>window.open('welcome.php','_self')</script>";
}
}
?>
希望这有帮助.. !!!