Есть $result->num_rows
всегда 0
в PHP
коде, хотя я пишу login
, который уже находится в базе данных.
Где я совершаю ошибку? Могу ли я напечатать что-то неправильное в fetch-запросе?
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<form action="" method="post">
<label >login</label>
<input name='username' id="input_login" type="text">
<span id="availability"></span>
<button>Submit</button>
</form>
<script src='https://code.jquery.com/jquery-3.3.1.min.js'></script>
<script src="index.js"></script>
</body>
</html>
JavaScript
const input_login = document.querySelector("#input_login");
const availability = document.querySelector("#availability");
input_login.onblur = async () => {
if(input_login.value != "") {
let username = input_login.value;
const response = await fetch("unique.php", {
method: 'POST',
body:{user_name:username}
})
const data = await response.text();
availability.innerHTML = await data;
}}
PHP
<?php
$connect = mysqli_connect("localhost", "root", "", "site");
$sql = "SELECT * FROM `users` WHERE `login` = '%".$_POST["user_name"]."%' ";
$result = $connect->query($sql);
if ($result->num_rows < 0) {
echo 'This username is already taken';
} else {
echo 'This username is available';
}