2016-06-18 8 views
0

を与える私はここで、任意のセッションなしから、単純なログに取り組んでいます私のPHPコードは次のとおりです。PHP LogingフォームMySQLとブートストラップスタイルがエラー

<?php 

    $mysqli = new mysqli('localhost', 'cshrnaf_user2', '=cXlIBsdMkdr', 'cshrnaf_mis_db'); 

if (isset($_POST['Username'])) { 
     $sql = "SELECT * FROM user WHERE email= '{$mysqli->real_escape_string($_POST['Username'])}' AND password= '{$mysqli->real_escape_string($_POST['Password'])}' LIMIT 1"; 
     $res = mysql_query($sql); 
     if (mysql_num_rows($res) == 1) { 
      echo "YOu have log in"; 
      exit(); 
     } else { 
      echo "not log in"; 
      exit(); 
     } 
} 
?> 

と私のブートストラップコード:

</head> 
<body class="gray-bg"> 
    <div class="middle-box text-center loginscreen animated fadeInDown"> 
     <div> 
      <h3>Welcome</h3> 
      <p>Login to your account.</p> 
      <form method="post" class="m-t" role="form"> 
       <div class="form-group"> 
        <input type="email" class="form-control" name=Username required=""> 
       </div> 
       <div class="form-group"> 
        <input type="password" class="form-control" name=Password required=""> 
       </div> 
       <button type="submit" class="btn btn-primary block full-width m-b">Login</button> 
      </form> 
     </div> 
    </div> 
    </body> 
</head> 

警告:するmysql_query():それは私に3エラーを与えるアクセス(パスワードを使用して:NO)、 'ユーザーのための' @ 'localhost' の拒否されなかっライン上/home/cc/public_html/MIS_cc/login.phpで7

警告:サーバへのリンクはライン7

警告に/home/cc/public_html/MIS_cc/login.phpで確立することができませんでした:()するmysql_queryはmysql_num_rows()は、パラメータ1はリソースであることを期待し、あなたが本当にとして準備されたステートメントを使用することを検討すべきであるしかし

<?php 

if (isset($_POST['Username'])) { 
    $sql = "SELECT * FROM user WHERE email= '{$mysqli->real_escape_string($_POST['Username'])}' AND password= '{$mysqli->real_escape_string($_POST['Password'])}' LIMIT 1"; 
    $res = mysqli_query($mysqli , $sql); // Edited this line 
    if (mysqli_num_rows($res) == 1) { //Edited this line 
     echo "YOu have log in"; 
     exit(); 
    } else { 
     echo "not log in"; 
     exit(); 
    } 
} 
?> 

:9行 に/home/cc/public_html/MIS_cc/login.phpに指定したbooleanがこれにあなたのコーディングの変更をするために

+0

あなたはNONOある 'mysql_ *'と 'mysqli_ *'を混合しています。クエリとnum_rowsをmysqliに変換すると、修正されます。 http://stackoverflow.com/questions/17498216/can-i-mix-mysql-apis-in-php – Matt

答えて

0

にログインできません文字列をエスケープするだけでは、SQLインジェクションから身を守ることはできません。あなたのコードを使用してプリペアドステートメントを使用しての

例:

<?php 

if(isset($_POST['Username'])) { 
    $username = $mysqli->real_escape_string($_POST['Username']); 
    $password = $mysqli->real_escape_string($_POST['Password']); 

    // Wrapping the prepared statement in an IF so if the sql statement is wrong it will return false and allows for error handling. 
    if ($stmt = $mysqli->prepare("SELECT * FROM user WHERE email = ? AND password = ? LIMIT 1")) { 

     // Bind the above $username and $password to the prepared query 
     $stmt->bind_param('ss', $username, $password); 

     // Execute the prepared query. 
     $stmt->execute() 

     // Store result of prepared statement 
     $stmt->store_result(); 

     if ($stmt->num_rows == 1) { 
      echo "You have logged in."; 
      exit(); 
     } else { 
      echo "Log in failed."; 
      exit(); 
     } 
     $stmt->close(); 
    } 
} 
+0

私はコードの友人を試してください。 2番目のコードでエラーが返されます:構文解析エラー:構文エラー。予期しない '$ stmt'(T_VARIABLE)/home/ccss/public_html/MIS_ccss/login.phpの17行目と1番目のコードでエラーが発生します。 致命的なエラー: /home/ccs/public_html/MIS_ccss/login.php 4行目の非オブジェクトのreal_escape_string() –

+0

@FarhadpaikanあなたはまだDB接続を持っていますか、それとも文字通りコピーして貼り付けましたか? – Matt

+0

はい私はちょうど今コピーして貼り付けています。DB接続で試してみました。ありがとうございました。) –