Yes, before you tell me to set the username column to primary key, so it is not allowed to duplicate. I'm already using the primary key for my user_id, which is on auto increment. I was hoping to be able to put a check in place in the php, that would see if the username has been taken already and send a json_encode back to my app. BTW, the database is being hosted online, so not really localhost!
This is what I'm using right now, that is not working:
<?php
$con = mysqli_connect("localhost", "id177667_root", "***", "id177667_loginb");
$name = $_POST["name"];
$username = $_POST["username"];
$password = $_POST["password"];
$response = array();
$dup = mysqli_query($con, "SELECT * FROM user WHERE username=$username");
if(mysqli_num_rows($dup) > 0) {
$response["success"] = false;
}
else {
$statement = mysqli_prepare($con, "INSERT INTO user (name, username, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $name, $username, $password);
mysqli_stmt_execute($statement);
$response["success"] = true;
}
echo json_encode($response);
?>
When I run the hosted link of this, I get success:true
, but it should be false:
<?php
$con = mysqli_connect("localhost", "id177667_root", "***", "id177667_loginb");
$name = $_POST["name"];
$username = $_POST["username"];
$password = $_POST["password"];
$response = array();
$statement = mysqli_prepare($con, "SELECT * FROM user WHERE username=?");
mysqli_stmt_bind_param($statement, "s", $username);
mysqli_stmt_execute($statement);
if(mysqli_num_rows($statement) > 0) {
$response["success"] = false;
}
else {
$statement = mysqli_prepare($con, "INSERT INTO user (name, username, password) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($statement, "sss", $name, $username, $password);
mysqli_stmt_execute($statement);
$response["success"] = true;
}
echo json_encode($response);
?>