2016-07-07 103 views
0

在收件箱中插入數據時,與mysql數據比較時會顯示錯誤信息。 我dbcon.php文件php索引值與sql數據庫值匹配不起作用

<?php 
 
define('DB_server','localhost'); 
 
define('DB_user','root'); 
 
define('DB_pass',''); 
 
define('DB_Name','dbtuts'); 
 

 
class DB_con 
 
{ 
 
\t function __construct() 
 
\t { 
 
\t $con=mysql_connect(DB_server,DB_user,DB_pass) or die('connection problem'.mysql_error()); 
 
\t mysql_select_db(DB_Name,$con); 
 
\t 
 
\t } 
 
\t 
 
\t public function comp($lnum) 
 
\t { 
 
\t $res = "SELECT * FROM users WHERE l_num= '".$_GET["$lnum"]."'"; 
 
\t return $res; 
 
\t } 
 
\t 
 
\t 
 
} 
 
?>

這裏數據庫表中的值f_num,l_num,和與我compare.php文件

<?php 
 
include_once 'dbcon.php'; 
 
$con=new DB_con(); 
 

 
if(isset($_POST['log'])) 
 
\t { 
 
\t 
 
\t $lnum=$_POST['li_num']; 
 
\t $res=$con->comp($lnum); 
 
\t if($res){ 
 
\t while($row = mysql_fetch_array($res)) { 
 
\t $num=$row['li_num']; 
 
\t 
 
\t ?> 
 
\t <script> 
 
\t 
 
\t alert("thnks for login") 
 
\t </script> 
 
\t <?php 
 
\t } 
 
\t } 
 
    } 
 
\t ?> 
 
<html> 
 
\t <head> 
 
\t <title>Enter customer value</title> 
 
\t </head> 
 
\t <body> 
 
\t <center> 
 
\t \t <form method="POST"> 
 
\t \t \t <table align="center"> 
 
\t \t \t <tr> 
 
\t \t \t <td><input type="text" name="fi_num" placeholder="enter value" required> name</td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t <td><input type="text" name="li_num" placeholder="enter extra"> password</td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t <td><input type="text" name="sum" placeholder="enter sum"> sum</td> 
 
\t \t \t </tr> 
 
\t \t \t <tr> 
 
\t \t \t <td><button type="submit" name="log">login</button></td> 
 
\t \t \t </tr> 
 
\t \t \t </table> 
 
\t \t </form> 
 
\t </center> 
 
\t </body> 
 
</html>

時我點擊登錄按鈕兩個錯誤來到

Notice: Undefined index in dbcon.php and 
Warning: mysql_fetch_array() expects parameter 1 to be resource, string given in compare.php 

任何建議,這problem.it將有助於我

+1

您需要使用mysqli_ * –

+0

$ lnum的值是多少? –

+1

你正在'發佈'表單,但'GET'變量。這是行不通的。並切換到mysqli或PDO。 mysql_ *函數已被棄用,並從PHP7開始被刪除。這兩個庫將允許您防止SQL注入。 – aynber

回答

-2

你補償功能不返回MySQL的結果集,它返回表示select語句的字符串。您需要將此字符串傳遞給mysql_query以返回結果集。其次,你傳遞「comp」$ _POST [$ li_num]的值並嘗試從$ _GET中獲取結果。從comp函數中移除$ _GET並直接在其中使用$ lnum。

很明顯,sql的衛生問題也需要解決。

public function comp($lnum) 
{ 
    return mysql_query("SELECT * FROM users WHERE l_num= '" . mysql_real_escape_string($lnum) . "'"); 
} 

您應該研究使用mysqli,因爲這些函數已被棄用。

+2

您的腳本存在[SQL注入攻擊]的風險(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql -injection-in-php) 看看發生了什麼到[小Bobby表](http://bobby-tables.com/)即使 [如果你逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that -gets-around-mysql-real-escape-string) 使用[prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly

+0

感謝您的快速響應..sql衛生方面的影響是爲了工作,但是有問題.with未定義的索引:li_num在比較。php –

+0

請更新您的問題以獲取更新的代碼和錯誤。 –