私は現在、私のコースの一環としてニュースサイトのログインシステムを作っています。何らかの理由でif文で$ rows-> num_rows == 1を使用すると、常に "else"コードが実行されます。基本的には、これは、入力されている正しいユーザー情報に対応するテーブルの行を検出できないことを意味します。 HTMLコードに情報が入力されたときに実行されるPHPコードを次に示します。テーブル内のNUMBER行が検出されない
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//Connect to DB
include_once("db_connect.php")or die ("Couldnt connect to DB");
$username = $_POST['user'];
$password = $_POST['password'];
session_start();
if(trim($username) != '' and trim($password) != ''){
//Sanitizes whatever is entered
$username=stripslashes($username);
$password=stripslashes($password);
$username=strip_tags($_POST['user']);
$password=strip_tags($_POST['password']);
$username=mysqli_real_escape_string($conn,$username);
$password=mysqli_real_escape_string($conn,$password);
//Checks whether Username exists
$query = mysqli_query($conn, "SELECT * FROM user WHERE users='$username'
AND password = '$password' ")
or die(mysqli_error($conn));
$numrows=mysqli_num_rows($query);
if($numrows > 0){
// echo "Record exists.";
$_SESSION['login_user']=$username; // Initializing Session
header("location: index.php"); // Redirecting To Other Page
exit;
}
else {
echo "Username or password is incorrect.";
}
}else{
echo "Please enter information";
}
?>
最後のif文で行が検出されないので問題が発生します。そして、はい、私のテーブルは1行のユーザー情報(ユーザー、パスワード)で埋められ、HTMLフォームもPOSTを使用します。
私はこの問題を少なくとも3時間は調査したが、まだ解決策を見つけることはできません。ここで
現在のエラーログです:
Warning: include_once(1): failed to open stream: No such file or directory in /home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 6
Warning: include_once(): Failed opening '1' for inclusion
(include_path='.:/usr/share/pear/') in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 6
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 22
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null
given in /home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line
22
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 23
Warning: mysqli_real_escape_string() expects parameter 1 to be mysqli, null
given in /home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line
23
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 42
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 42
Notice: Undefined variable: conn in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 43
Warning: mysqli_error() expects parameter 1 to be mysqli, null given in
/home/vol9_7/byethost4.com/b4_18083024/htdocs/loginAuth.php on line 43
EDIT:フレッド-ii-答えを使用します。 include_once("db_connect.php")or die ("Couldnt connect to DB");
がコードの先頭に移動しました。
第2に、新しいバージョンのif文が追加されて古いバージョンに置き換えられました。このステートメントはFred -ii- answerでも見ることができます。私は、テーブルと列 名前を混同しているので
第三に、SQLステートメントが修正されました。
最後に、error_reporting(E_ALL); ini_set('display_errors', 1);
はフレッド-ii-答えの礼儀再び、エラーを見つけるのを助けるために追加されました。
'IF($ rows-> NUM_ROWS> = 1)'旋回を与えます。重複したユーザー名に賭けているか、クエリが失敗しました。 –
oh btw 'include_once(" db_connect.php ")'は間違った場所にあり、同じ名前を持つ複数のユーザ名がある場合でも私の上記のコメントがおそらく使用されるべきですが、その前にまず接続する必要がありますエスケープ関数。あなたはここで馬の前に馬車を置き、何もエラーをチェックしていません。 –
データベースにレコードが重複していますか? PHP文書の最初の行にinclude_once( "db_connect.php")を移動してください –