2016-07-11 9 views
-1
ログインページでSQLインジェクション

の防止しますMYSQL接続作る:mysqli_prepareを使用することによりは、私は、ログインページ(<code>login.php</code>)を持っており、以下、私は<code>mysqli_prepare</code>を使用してユーザーを認証するために使用しようとしていますコードでPHP

function connect_database() 
{ 
    $con = mysqli_connect("servername", "username", "", "dbname"); 
    if (!$con) 
    { 
     $con = ""; 
     echo("Database connection failed: " . mysqli_connect_error()); 
    } 
    return $con; 
} 

を、私は次の警告やエラーメッセージを取得しています:

Warning: mysqli_prepare() expects exactly 2 parameters, 1 given 

Fatal error: Call to a member function bindParam() on null 

私は2日前に同様の質問をしましたが、完全な解決策はありませんでした。 (Prevent SQL Injection to Login Page in PHP and MySQL)。

問題の解決にお手伝いをしてください。 DBをSQLインジェクションから防ぐための最良の方法ですか?

+0

mysqli_prepare($ connection、 "SELECT * FROM table"); 'のように、mysqli_prepare()に2番目の引数を指定する必要があります。 –

+0

接続変数$ query = mysqli_prepare($ con、 "user_name =?かつpassword =?"というユーザからuser_idを選択します)がありません。 – JYoThI

+0

'bindParam'でmysqliとPDOを混在させる – Saty

答えて

1

とこれも

$query->bind_Param("ss", $unsafe_username, $unsafe_password); 
      ^^^ 
+0

変更を加えることで、今このエラーが発生します:致命的なエラー:未定義のメソッドを呼び出すmysqli_stmt :: bindParam() –

+0

@KPJあなたの情報を取得する場所はわかりませんが、異なるデータベースAPI間でのマッチングmysqliは実際には 'bindParam'を呼び出すことはありません。それは 'bind_param'と呼ばれるものを持っています。繰り返しますが、マニュアルを実際に読んでそこに示されている例に固執する必要があります:http://php.net/mysqli_prepare – deceze

+0

私の答えは今更新されました@KPJoy – JYoThI

1

あなたは準備機能で$con変数を追加し、変数接続$con

$query=mysqli_prepare($con,"select user_id from users where user_name= ? and 
password= ? ");  

を逃す

$query=mysqli_prepare($con,"SELECT user_id FROM users WHERE user_name= ? AND password= ? ");

1

手続きSTYを変更する必要がありますleのmysqli_prepareは、最初のパラメータが接続変数で、2番目が文字列(クエリ)であると想定しています。

変更

mysqli_prepare("select user_id from users where user_name= ? and password= ? "); 

mysqli_prepare($conn, "select user_id from users where user_name= ? and password= ? "); 

documentation hereをお読みください。