2016-11-15 25 views
-2
if(isset($_POST['submit'])){ 
       $uname=$_POST['username']; 
       $pwd=$_POST['password']; 
       $acc_type=$_POST['acc_type']; 
       $_SESSION['user_type']=$acc_type; 

       if($acc_type=='Teacher'){ 
        $sql="select userid,password from teacherinfo where userid='$uname'"; 
       } 
       else if($acc_type=='Student'){ 
        $sql="select userid,password from studentinfo where userid='$uname'"; 
       } 
       else if($acc_type=='Admin'){ 
        $sql="select userid,password from admininfo where userid='$uname'"; 
       } 

       $query = mysql_query($sql); 
       $count = mysql_num_rows($query); 
       if($count>0){ 
        $row_data = mysql_fetch_row($query); 
        if($row_data[1]==$pwd){ 
         $_SESSION['userid']=$row_data[0]; 
         $url="profile.php"; 
         header("Location:$url"); 
        } 
        else{ 
         echo "Password Miss match!"; 
        } 
       } 
       else{ 
        echo "User not Found!"; 
       } 



      } 

注意:未定義的變量:sql在C:\ xampp \ htdocs \ MJ \ index.php在第39行警告:mysql_num_rows()期望參數1是資源,在線40上的C:\ xampp \ htdocs \ MJ \ index.php任何人都可以幫助我嗎?它的一個登錄表單,但它給出的錯誤

+2

[PHP:「Notice:Undefined variable」和「Notice:Undefined index」]的可能重複(http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-索引) – Henders

+0

使用mysqli_ *或pdo,mysql *已被棄用,並會給你警告。 –

+2

你**不能**使用mysql_xxx函數,這些函數從PHP5.5開始不推薦使用,並且在PHP7中刪除。使用mysqli_xxx或PDO代替:http://php.net/manual/en/intro.mysql.php。此外,**從不**直接在您的SQL查詢中使用用戶輸入:http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php –

回答

2

從PHP網站查看代碼,您並未將您的sql語句鏈接到您對數據庫建立的連接。看下面的代碼,你會看到一個叫做$link的變量是創建的,然後提供了要使用的數據庫,然後作爲第二個變量放入sql語句變量$result中。

<?php 

$link = mysql_connect("localhost", "mysql_user", "mysql_password"); 
mysql_select_db("database", $link); 

$result = mysql_query("SELECT * FROM table1", $link); 
$num_rows = mysql_num_rows($result); 

echo "$num_rows Rows\n"; 

?> 

你真的這麼做了,作爲註釋狀態,必須停止使用MySQL和移動到PDO,這個網站應該爲你提供足夠的信息,讓您開始,將固定語句發送到數據庫 - https://code.tutsplus.com/tutorials/why-you-should-be-using-phps-pdo-for-database-access--net-12059

除此之外,您還需要查看哈希密碼,目前您使用純文本,這是不安全的。使用類似password_hash() - http://php.net/manual/en/function.password-hash.php - 將提供更安全的密碼存儲方式。一旦存儲了安全保護,您可以使用password_verify()將來針對提供的密碼進行檢查。

+0

鏈接標識符是一個可選參數,如果之前調用過mysql_connect,則該鏈接將用於查詢http://php.net/manual/en/function.mysql-query.php –

相關問題