Okay so my problem here is that I try to make a login page for a website and it lets me login with wrong username and password, the js part is the one I think I have a problem with but here are my codes for the 3 things where there may be some problems
JS on the login.php:
<script type="text/javascript">
$(function(){
$('#login').click(function(e){
var valid = this.form.checkValidity();
if (valid) {
var username = $('#username').val();
var password = $('#password').val();
e.preventDefault();
$.post('jslogin.php',
{
username: username,
password: password
},
function(data,status){
if (status === 'success') {
Swal.fire(
'Successful',
'You logged in successfully',
'success'
).then(function() {
window.location = "index.php";
})
}else{
Swal.fire(
'Error',
"We couldn't log you in with what you just entered!",
'error'
).then(function() {
window.location - "login.php";
})
}
});
}else {
}
})
});
jslogin.php from the ajax command
<?php
require_once('config.php');
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username = ? AND password = ? LIMIT 1";
$stmtselect = $db->prepare($sql);
$result = $stmtselect->execute([$username, $password]);
if ($result) {
$user = $stmtselect->fetch(PDO::FETCH_ASSOC);
if ($stmtselect->rowCount() > 0) {
echo '1';
}else{
echo 'there is no user for what you have entered!';
}
}
else{
echo 'error at connecting to database';
}
?>
config.php from jslogin.php
<?php
$db_user = "root";
$db_pass = "";
$db_name = "user accounts";
$db = new PDO('mysql:host=localhost;dbname=' . $db_name . ';charset=utf8', $db_user, $db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);