I know this might be a duplicate on:
php - MYSQLI_NUM_ROWS ALWAYS RETURNING 0
php - mysqli_num_rows() is always returning 0
The thing is none of them is giving me an answer for my problem.
This is an example code of my code with the same syntaxes as I used.
index.html
<html>
<head><title>GAS - Give Away System - Create User</title></head>
<body>
<script src="jquery.js"></script>
<input type="text" placeholder="Username" id="uname" /><br><br>
<p id="errors"></p>
<script>
$(document).ready(function(){
$.ajaxSetup({cache:false});
setInterval(function(){
$("#errors").load("php/errorchecking.php?c=username&v="+document.getElementById('uname').value);
}, 500);
});
</script>
</body>
</html>
php/errorchecking.php
<?php
$con = mysqli_connect('localhost', 'root', '', 'gas');
if(isset($_GET['c']) && isset($_GET['v'])){
echo 'Current value: ', $_GET['v'], '<br>This value is set for: ', $_GET['c'], '<br><br>';
if($_GET['c']==="username"){
if($_GET['v']===null){
echo "Sorry, this username is empty! Please write a username!";
}else{
// I know this is open for SQL Injection, it's not in my worries.
$query = "SELECT `username` FROM `users` WHERE `username` ='{$_GET['v']}'";
$sql = $con->query($query);
if(mysqli_num_rows($sql) > 0){
echo "Sorry, this username is already in use! Please choose something else";
}else{
echo "Username avaible!"; //This is line 17
}
}
}
}else{
echo 'This is an invalid form!';
}
?>
Now lets say I have a username in my table called User15, and someone's input is the exact same it will display the message "Username available!" from php/errorchecking.php Line:17
Why does it do that? Since it already is a user there called User15, so what it should display is "Sorry, this username is already in use! Please choose something else"
Thanks for taking time helping me! Cecilie.
usersWHERE username='".$_GET['v']."'";:)I gave a full explanation. That counts.:)You didn't explain why it happens, and we both have a difference of12 seconds, which I used to explain it well. An answer should not be just do this and you'll get it, will be like you don't teach the person.