dpict99695329 2011-10-26 22:58 采纳率: 100%
浏览 9
已采纳

无法获取表单发布或查询不起作用

I'm trying to learn about $_SESSION variables and I've been following some tutorials I've found online. I've created a form and I'm posting it back to the same page but the query fails. I have a table 'users' and I've added 'bob' to the userlogin field and 'bob' to the user_passwrd field. When I execute I get the error from the if/else statement that the query failed. I don't know what I'm doing wrong and I would really appreciate some help on this. I don't know if the form is not posting or where the problem might be. Here is the code:

<?php
    include ('admin_pages/sql_pages/sql_connect_L.inc.php');

    $login = mysql_real_escape_string($_POST['login']);
    $passwrd = mysql_real_escape_string($_POST['passwrd']);

    $sql = mysql_query("SELECT * FROM users WHERE userlogin = '$login' AND user_passwrd = '$passwrd'");

    $count = mysql_num_rows($sql);

    if($count == 1)
    {
        session_start();
        $_SESSION['login'] = '$login';
        header('location: admin_pages/dashboard.php');
    }

    else
    {
        echo " Your query failed";
    }

?>

    <form method="post" action="logintest.php">
      Username: <input name="login" type="text" class="login_fields" />
      <br/>
      Password: <input name="passwrd" type="password" class="login_fields" />
      <br/>
      <input name="submit" type="submit" class="long_button" id="submit" value="Login" />
    </form>

I've tried several different things and I can't get the desired results.

Cheers

  • 写回答

1条回答 默认 最新

  • douji5523 2011-10-26 23:05
    关注

    The first step in debugging is, check your SQL string:

    $sqlText = "SELECT * FROM users WHERE userlogin = '$login' AND user_passwrd = '$passwrd'";
    echo $sqlText;
    $sql = mysql_query($sqlText) or die(mysql_error());
    

    I noticed, you have minor issue in your code:

    $_SESSION['login'] = '$login';
    

    will result in $_SESSION['login'] having value of $login literally. Not the value of $login. To fix it, change your code into:

    $_SESSION['login'] = "$login";
    //or to be simpler:
    $_SESSION['login'] = $login;
    

    Why is that? here's the explanations.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b