Problem preventing registration of duplicate users PHP - MYSQL

2

I have an Android application where I access a MySql database by php.

I am trying to make that when a user tries to register and already exists, he does not insert the data into the database.

The problem is that by putting the if that counts the rows of select or whatever since I've tried enough select , it never executes the insert that is inside the if .

What I have commented are some of the options with which I have also tried without success.

On the other hand, in the database I have primary keys that the phpadmin allows to insert duplicates without any problem.

Code:

<?php

$connect = mysqli_connect("localhost","root","root","comuniero");

if(mysqli_connect_errno($connect))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
    echo "success";
}

$email = isset($_POST['email']) ? $_POST['email'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';


/*mysqli_select_db($connect,"comuniero");*/
$sql= mysqli_query($connect,"SELECT COUNT(*) AS total FROM users WHERE email='$email'");
$row=mysqli_fetch_object($sql);
if($row->total > 0){
    $query = mysqli_query($connect, "insert into users (username,email,password) values ('$email','$email','$password') ");
}
/*
$sql = "SELECT * FROM users WHERE email='$email' ";
$result=mysqli_query($connect,$sql) or die (mysql_error());
if(mysqli_num_rows($result)>0){
        $query = mysqli_query($connect, "insert into users (username,email,password) values ('$email','$email','$password') ");
}
}
/*$result = $connect->query($sql);

if ($result->num_rows > 0) {
    $query = mysqli_query($connect, "insert into users (username,email,password) values ('$email','$email','$password') ");*/
    /*
$n1=1;
$n2=1;
if ($n1==$n2) {
    $query = mysqli_query($connect, "insert into users (username,email,password) values ('$email','$email','$password') ");
*/
else {
    echo "0 results";
}

mysqli_close($connect);
?>
    
asked by Nanako3 07.09.2016 в 12:00
source

2 answers

1

Indeed, as Alvaro says, the condition is reversed. Another option would be to simply pass the INSERT to the else you have below.

On the other hand, you could save $row=mysqli_fetch_object($sql); by using for the condition of if the following function: if(mysqli_num_rows($sql)>0) (leaving the condition as it is in your example, which I repeat is wrong)

Finally, tell you that if you actually have a PRIMARY KEY defined (if it is the username or email) you could save all the checking using

insert ignore into users (username,email,password) values ('$email','$email','$password')
    
answered by 07.09.2016 / 15:09
source
1

A problem that will cause the insert that is inside the if not to run is that the condition is set backwards as it should be: you want the data to be inserted if there is no record, and if it already exists, do not insert the data in the database. Then the insert should be made if the result of the COUNT(*) is 0, but in the if it is being checked that it is greater than 0:

$sql= mysqli_query($connect,"SELECT COUNT(*) AS total FROM users WHERE email='$email'");
$row=mysqli_fetch_object($sql);
if($row->total > 0){
    // insert va aquí, pero sólo se ejecuta si hay un usuario con ese email
}

That will only insert into the database if there is already a record with that email, which is what you want to avoid. Instead you should check that $row->total is 0. With that the insert will be made exclusively when there is no longer a user with that email:

$sql= mysqli_query($connect,"SELECT COUNT(*) AS total FROM users WHERE email='$email'");
$row=mysqli_fetch_object($sql);
if($row->total == 0){
    // insert va aquí, y sólo se ejecuta si no hay ningún usuario con ese email
}
    
answered by 07.09.2016 в 13:36