2017-12-03 9 views
-4

私はこのリンクの内部にある事前にコーディングされたプロジェクトを使用して実験を行っていますhere無効なSQLステートメント

私はxamppをmysqlのWebサーバーとして使用しています。このコードを持つ認証ページを実行するたびに:

<?php 
include_once("zSessionStart.php"); 
include_once("zConfig.php"); 
include_once("zDB.php"); 


$password = $_REQUEST["password"]; 
$userID = $_REQUEST["userID"]; 
$isAdmin = false; //is administrator or not 
$askDemo = false; 


    //authenticate the login data from login.php 

    $query="select userID, isAdmin, askDemo from $_usersTable where (password= '$password') and (userID='$userID');"; 
    $rs=mysql_query($query) or die ("Invalid sql."); 
      if (mysql_num_rows($rs) > 0) //correct password 
      { 
          $array = mysql_fetch_array($rs); 
          if(strcmp($array["isAdmin"], "y")==0){ 
           $isAdmin=true; 
          } 

          if(strcmp($array["askDemo"], "y")==0){ 
           $askDemo=true; 
          } 

          if(!$isAdmin){ 
           $query="select userID from usertests where userID='" . $userID . "'"; 
           $rs2=mysql_query($query) or die ("Invalid sql."); 
           if(mysql_num_rows($rs2) > 0){   //already take test 
            $array2 = mysql_fetch_array($rs2); 
            echo "<h2> You have already taken the task. Please contact your administrator if you " . 
              " feel you need to re-take this task again.</h2>"; 
            die(); 
           } 
          } 


          $_SESSION['userID'] = $array["userID"]; 
          session_unregister('loginErr'); 


          if($isAdmin){ 
           header("Location: transfer.php?" . SID); 
          }else{ 
           if($askDemo){ 
            header("Location:demo.php?" . SID); 
           }else{ 
            header("Location: index.php?" . SID);        
           } 
          } 
      } 
      else 
      { 
         $_SESSION['loginErr'] = "true"; 
         header("Location: login.php"); 
      } 



    mysql_close($db); 
?> 

「Invalid sql。」というエラーが表示されます。私のデータベースの中には、ユーザーIDとパスワードなどの資格情報を持つユーザーと呼ばれるテーブルがあります。私はすでにユーザー名をadminとパスワードに設定しました。しかし、私は問題が何であるかを理解する運がなかった。

+4

Gahです。現代のPHPでは 'mysql_ *'関数はなくなっています。彼らは単に存在しません。実際には、より現代的なチュートリアルでやり直すべきです。これには大きなセキュリティ上の脆弱性もあります。 – ceejayoz

+0

あなたのテーブルが間違っているように見えるので、無効なsqlが来ています。$ _usersTable –

+0

@ceejayoz、私はウェブ上にこれを設定しないので、セキュリティについてはあまり気にしません。私は古いPHP Webサーバーを使用することをお勧めしますか? – Yahyaotaif

答えて

1

変数の構文がなくても間違ったテーブル名、つまり$_usersTableを使用しているため、クエリが無効です。また、複数のセミコロンでクエリを終了していて、一重引用符も二重引用符でも正しく使用されていません。

$query="select userID, isAdmin, askDemo from usersTable where password = '".$password."' and userID ='".$userID."'; 

は、私は強くあなたはそれがよりセキュア以下のようにするためにmysqliの準備書にクエリを使用することをお勧めします。

あなたは以下のあなたの問題を解決するようなあなたの選択クエリを更新する必要があります

$mysqli = mysqli_connect($host, $username, $password, $db); 
$query = "SELECT userID, isAdmin, askDemo from `usersTable` WHERE userID=?"; 
$stmt = $mysqli->prepare($query); 
$stmt->bind_param("i", $userID); 
$stmt->execute(); 
$res = $stmt->get_result(); 
$data = $res->fetch_all(); 

詳細については、リンクhttp://php.net/manual/en/mysqli.prepare.php

+0

@ Amit Gupta、お返事ありがとうございます。私はまだこれでどこにも行かなかった。コードは私のものではありません。それは研究グループによって大学の一つで開発されました。あなたが書いたクエリを使用しましたが、それ以降の行にはエラーが発生します。エラー:構文解析エラー:予期せぬ「Invalid」(T_STRING)の/Applications/XAMPP/xamppfiles/htdocs/verbal/authLogin.php(16行目)。MySqli prepared statementsをこのコードに配置することをお勧めします。 Xampp 1.8.2-6をMac用に使用しています。 – Yahyaotaif

関連する問題