首页 > 解决方案 > SQLSTATE[42000],未定义密码

问题描述

我有一个小问题,因为当我尝试登录我的页面时出现两个错误:其中一个是未定义的密码(我检查了 html 表单和其他可能出现问题但没有找到任何东西的来源。)第二个一个是“致命错误:未捕获的异常 'PDOException' 并带有消息 'SQLSTATE [42000]:

<!DOCTYPE html>
<html>
<head>
<title> Strona o nalewkach </title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<link rel="stylesheet" href="https://pro.fontawesome.com/releases/v5.10.0/css/all.css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="container h-100">
  <div class="d-flex justify-content-center h-100">
    <div class="user_card">
      <div class="d-flex justify-content-center">
         <div class="brand_logo_container">
             <img src= "img/logo.png" class="brand_logo" alt="Nalewki">
         </div>
      </div>
      <div class="d-flex justify-content-center form_container">
        <form>
        <div class="input-group mb-3">
         <div class="input-group-append">
           <span class="input-group-text"><i class="fas fa-user"></i></span>
           </div>
           <input type="text" name="username" id="username" class="form-control input_user" required>
           </div>
        
        <div class="input-group mb-2">  
         <div class="input-group-append">
           <span class="input-group-text"><i class="fas fa-key"></i></span>
         </div>
           <input type="password" name="password" id="password" class="form-control input_pass" required>
        </div>
    
    <div class="form-group">
    <div class="custom-control custom-checkbox">
      <input type="checkbox" name="rememberme" class="custom-control-input" id="customControlInLine">
      <label class="custom-control-label" for="customControlInLine">Zapamiętaj mnie</label>
      </div>
     </div>
    </div>
    <div class="d-flex justify-content-center mt-3 login-container">
    </div>
     </form>
    <div class="mt-4"> 
        <div class="d-flex justify-content-center links">
    Nie posiadasz konta? <a href="#" class="ml-2">Zarejestruj sie</a>
    <script>
    $(function(){
    $('#login').click(function(e){
        var valid = this.form.checkValidity();
        if(valid){
            var username = $('#username').val();
            var password = $('password').val();
        }
        e.preventDefault();
    $.ajax({
    type: 'POST',
            url: 'jslogin.php',
            data: {username: username, password: password},
            success: function(data){
                alert(data);
                if ($.trim(data)==="1"){
                    setTimeout(' window.location.href = "index.php"',2000);
                }
            },
            error: function(data){
                alert('wystapil blad');
            }
    });
    });
    });
    </script>
    </body>

    </html>

jslogin.php:

    <?php
require_once('config.php');

$username = $_POST['username'];

$password = $_POST['password'];




$sql = "SELECT  * FROM useraccounts.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'Nie znaleziono uzytkownika';
    }
    }else{
        

echo'Wystapily bledy przy laczeniu z baza danych';
        

    }
        ?>

标签: javascriptphphtml

解决方案


在 jslogin.php 中,像这样更改 POST 部分:

$username = '';
$password = '';

if (isset($_POST['username'], $_POST['password'])) {
  $username = $_POST['username'];
  $password = $_POST['password'];
}

此外,在 JavaScript 代码中,在密码行中添加尖号:

if(valid){
    var username = $('#username').val();
    // You forgot to add sharp here
    // var password = $('password').val();
    // Correct one
    var password = $('#password').val();
}

我对isset()有同样的问题,我像这样解决了它。


推荐阅读