无法使用PHP中的用户名和密码登录

时间:2014-12-13 07:36:51

标签: php mysql

<?php
//Get Session
$getSession = isset($_SESSION['forum_user']) ? $_SESSION['forum_user'] : "";

//Login Script
$postLoginBtn = isset($_POST['forum_login_submit']) ? $_POST['forum_login_submit'] : "";

if ($postLoginBtn == "Login") {
    $user = isset($_POST['forum_user']) ? $_POST['forum_user'] : "";
    $pass = isset($_POST['forum_pass']) ? $_POST['forum_pass'] : "";
    $pass = md5($pass);
    $query = mysql_query("SELECT * FROM admin_users WHERE user_name='{$user}' and password='{$pass}'");
    echo "SELECT * FROM admin_users WHERE user_name='{$user}' and user_password ='{$pass}'";
    $count = mysql_num_rows($query);
    //echo $count;
    if ($count == 1) {
        $_SESSION['forum_user'] = $_POST['forum_user'];
        header("Location:admin.php");
    } else {
        echo "<script type='text/javascript'>";
          echo "alert('Entered username and password is not matched. Please try again!!');";
          echo "window.location='index.php';";
          echo "</script>"; 
    }
}

if ($getSession == "") {
    ?>
    <div id="loginContainer">
        <div id="loginDiv">
            <div id="loginHeadingDiv">LOGIN PANEL</div>
            <div id="loginFieldsDiv">
                <form action="" method="post">
                    <table id="loginTable">
                        <tr>
                            <td>Username</td>
                            <td><input type="text" class="loginFields" name="forum_user" /></td>
                        </tr>
                        <tr>
                            <td>Password</td>
                            <td><input type="password" class="loginFields" name="forum_pass" /></td>
                        </tr>
                        <tr>
                            <td></td>
                            <td><input type="submit" id="loginSbmtBtn" value="Login" name="forum_login_submit" /></td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </div>

    <?php
} else {
    echo "<script type='text/javascript'>";
    echo "alert('Kindly login to enter the admin area.');";
    echo "window.location='index.php';";
    echo "</script>";
}
?>

我正在尝试登录但我收到错误&#34;警告:mysql_num_rows()期望参数1是资源,boolean在第15行和第34行给出login.php ;;。T尝试了很多但是无法解决请帮帮我。

1 个答案:

答案 0 :(得分:0)

试试这个 - &gt;

    $res="write your select query here" ;
    $result=mysql_query($res) or die("query fail to execute".mysql_error());
    $total=mysql_num_rows($result );
    if( $total<0){
     echo "no row found";
     }
    else{
     while($row=mysql_fetch_array($result))
     {
   // some code}