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);
?>