-3
<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>";  
    }  



}  

?> 
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

1 Answers1

0

As per the Code Observed that

1) You are trying to fetch records for new users whose account doesn't exist in the DB. So If you enter new Username, Email , Password It will automatically goes to

die('Connection error');

Because count will be '0' here. So PHP script Execution stops and the record will not be inserted in DB.

Try this Below Code. !!

<?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>";  
            } 
}  

?> 

Hope this helps..!!!

Raghav
  • 388
  • 1
  • 12