<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>";
}
}
?>
Asked
Active
Viewed 36 times
-3

Alive to die - Anant
- 70,531
- 10
- 51
- 98
-
2And what is the problem/Question here? – Alive to die - Anant Nov 23 '17 at 10:04
-
2What details? On what table? – Eddie Nov 23 '17 at 10:05
-
even when i submit i get the eroor"connecting to database – kayjordan Nov 23 '17 at 10:06
-
3Your script is at risk of [SQL Injection Attack](https://stackoverflow.com/q/60174/5914775). Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/). Even [if you are escaping inputs, its not safe!](https://stackoverflow.com/q/5741187/5914775). Use [prepared parameterized statements](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php) instead. – Tom Udding Nov 23 '17 at 10:06
-
`or die(mysqli_connect_error());` try this so that you will get exact connection error details and able to rectify that.Make sure all parameters provided for connection is correct – Alive to die - Anant Nov 23 '17 at 10:07
-
the login table with columns username paswword and email – kayjordan Nov 23 '17 at 10:08
-
or die(mysqli_connect_error()); didnt help – kayjordan Nov 23 '17 at 10:10
-
1ok @TomUdding i will do that – kayjordan Nov 23 '17 at 10:12
1 Answers
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
-
-
when i enter details of existing user and submit the page shows no error and just reloads – kayjordan Nov 23 '17 at 11:04
-
ur code helped @raghav so thanks a lot but the only error im getting is the one with existing user check – kayjordan Nov 23 '17 at 11:07
-
-
Fatal error: Call to a member function fetch_array() on boolean in C:\wamp64\www\try\login.php on line 38 <==this is the error i got @raghav – kayjordan Nov 23 '17 at 16:05