dtyqflrr775518 2014-07-09 14:22 采纳率: 100%
浏览 16

使用bootstrap模式和jquery时,mysql_fetch_array不起作用

I'm using ajax $.post method to send user and pass to my database and everything looks good but mysql_fetch_array does not fetch data from database. here is the jquery:

$(document).ready(function(){
    $('#but').click(function(){
        //$('#load').html('<strong>در حال ارسال پیام، لطفا منتظر باشید</script>');
    var pcode = $('#pcode').val();
    var pass = $('#pass').val();
    $.post('login_ajax.php',{php_pcode:pcode,php_pass:pass,su:"set"},function(data){$('#load').html(data);});
    });
});

and here is the php code:

<?php
if (isset($_POST['su'])) {
$pcode = $_POST['php_pcode'];
$pass = $_POST['php_pass'];
require_once('../inc/db.php');
$res = mysql_query("SELECT * FROM user WHERE pcode='$pcode' AND passs='$pass'");
$fe = mysql_fetch_assoc($res);
$co = mysql_num_rows($res);
if ($co == 1) {
    $_SESSION['pcode'] = $pcode;
    $_SESSION['pass'] = $pass;
    $_SESSION['firstname'] = $fe['firstname'];
    $_SESSION['lastname'] = $fe['lastname'];
    $_SESSION['sex'] = $fe['sex'];
    $_SESSION['level'] = $fe['level'];
        if ($_SESSION['level'] == 1) {
            header('location: admin_panel.php');
            }
        elseif ($_SESSION['level'] == 2) {
            header('location: elementary_panel.php');
            }
        elseif ($_SESSION['level'] == 3) {
            header('location: guidance_panel.php');
            }
        elseif ($_SESSION['level'] == 4) {
            header('location: highschol_panel.php');
            }
        elseif ($_SESSION['level'] == 5) {
            header('location: art_panel.php');
            }
        }
    elseif ($count==0){
        echo "کد کاربری اشتباه است";
                //$_SESSION['msg'] = "کد پرسنلی یا رمز عبور اشتباه است";
                //header('location: login.php');
    }
}

?>

  • 写回答

1条回答 默认 最新

  • douxin8383 2014-07-09 14:26
    关注

    You have multiple bugs:

    1) Vulnerable to [SQL injection attacks(http://bobby-tables.com)
    2) A total lack of error handling on your queries. Never assume success. Always assume failure and treat success as a pleasant surprise. Absolute bare-bones minimal "safe" coding would be something like:

    $result = mysql_query($sql) or die(mysql_error());
                               ^^^^^^^^^^^^^^^^^^^^^^^
    

    3) Using an obsolete/deprecated database interface (mysql_*() functions should NOT be used anymore)
    4) Mis-matched variable names:

    $co = mysql_num_rows($res);
     ^^---the count
    if ($count == 1) {
         ^^^^^---where did this variable come from?
    

    Since you're checking the wrong value, ALL of your $_SESSION building code will never ever get executed, so the entirety of your script is useless.

    5) No call to session_start() anywhere, so your $_SESSION variables will be lost when the script exits.

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题