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 : :
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.
That's a wrong way of query syntax. You need to use back-ticks and not '
s:
$query = "SELECT username FROM `users` WHERE 'username'='".$_GET['v']."'";
//-------------------------------------------^--------^
Change it to:
$query = "SELECT `username` FROM `users` WHERE `username`='".$_GET['v']."'";
Note:
``
- For columns. ''
- For values. You have to remove '
(single quotes) around your column-name from your query like below:-
$query = "SELECT `username` FROM `users` WHERE `username`='".$_GET['v']."'";
Note:- Instead of '
(single quotes) use back-ticks. because single-quotes( '
) used for values and back-ticks used for column-name
as well as table-name
too.Thanks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.