H_MZ 2015-02-27 17:56 采纳率: 0%
浏览 18

Ajax仍然获取数据

I've been trying to find what's causing my code to still get data even if the query is false. It is a simple log in form using ajax.

Here is my code:

$(function() {
  $("#submit_login").click(function() {
    var username = $("input#username").val();
    if (username == "") {
       $('#error').html('Please Insert Your Email Address');    
       return false;
    }
    var password = $("input#password").val();
    if (password == "") {
       $('#error').html('Please Insert Your Password'); 
       return false;
    }
    var dataString = 'username='+ username + '&password=' + password;
    $.ajax({
      type: "POST",
      url: 'login.php',
      data: dataString,
      dataType: "html",
      beforeSend: function(){ $("#submit_login").val('Connecting...');},
      success: function(data) {
      if (data == 0) {
      $('#error').html('Wrong Login Data')
      $("#submit_login").val('Sign in!');
        } else {
            $('#error').html('<b style="color:green;">you are logged. wait for redirection</b>');   
            document.location.href = 'private.php'; 
        }
      }
     });
    return false;
    });
});

Login.php

<?php
session_start();
include("config2.php");
if(isset($_POST['username']) && isset($_POST['password']))
{
// username and password sent from Form
$username=mysqli_real_escape_string($db,$_POST['username']); 
$password=md5(mysqli_real_escape_string($db,$_POST['password'])); 
$results = $mysqli->query("SELECT id FROM users WHERE username='$username' and password='$password'");
$obj = $results->fetch_object();
if ($results)
{
$_SESSION['username'] = $obj->id;
        echo $username;
}


}

?>

The thing is, every time an attempt to log in happens, it redirect me to private.php even the username and password are incorrect. This means that the login.php still gets data even though the query return is false or 0.

I'm having a hard time finding out what is causing this and hopefully you guys can help me out! cheers.

  • 写回答

1条回答 默认 最新

  • weixin_33704234 2015-02-27 18:04
    关注

    If condition in login.php probably false. Should it be like this?? The variable $results always return 1 because you hold the query and not the results itself. Instead of $result, change to $obj.

    if ($obj)
    {
        $_SESSION['username'] = $obj->id;
        echo $username; 
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答