2017-07-08 7 views
-1

私はPHPを初めて使用しています。私はいくつかの問題があります。私は($ _GET)変数を持つユーザー名の値を取得します。ある人がurlに(mehwish)を入力したとしましょう。ユーザーが入力したユーザー名がデータベースに存在するかどうかを確認したい。私の下のコードは動作しません。私の間違いを修正してください。ご協力ありがとうございます。このようなユーザー名はDataabaseから取得していません

<?php include("header.php"); 
    include("classes/db.php"); 
    session_start(); 
    $username=""; 
    if(isset($_GET['username'])) 
    { 
     $user=$_GET['username']; 
     if("SELECT user_name FROM user_reg WHERE user_name='$user'") 
     { 
      $username="SELECT user_name FROM user_reg WHERE user_name='$user'"; 
      $run_username=mysqli_query($con,$username); 
      $row=mysqli_fetch_array($run_username); 
      $username=$row['user_name']; 
     } 
     else 
     { 
      die("User does nto exist"); 
     } 
    } 
    ?> 
+0

**危険**:あなたは** [SQLインジェクション攻撃](http://bobby-tables.com/)**に対して脆弱です** 60174/best-way-to-prevent-sql-injection-in-php)をご利用ください。 – Quentin

+0

申し訳ありません実際に私はこの分野の初心者であり、SQLインジェクションについてはわかりません。あなたはそれを勉強するためのコースやビデオのリンクを教えてください。あなたに感謝します。 –

+0

私の前のコメントのリンクを試してください。 – Quentin

答えて

0

使用何か:

<?php 
session_start(); //This always need to be first line of code 
include("header.php"); 
include("classes/db.php"); 

$username=""; 
if(isset($_GET['username'])) 
{ 
    $user = mysqli_real_escape_string($con,$_GET['username']); 
    //get data from table using the passed user name in the url 
    $checkUser = mysqli_query($con,"SELECT user_name FROM user_reg WHERE user_name='$user'") or die(mysqli_error($con)); 
    //return number of rows from the above query 
    //if more than 0 means user exist in database table 
    if(mysqli_num_rows($checkUser)>0) 
    { 
     $row = mysqli_fetch_array($checkUser); 
     $username = $row['user_name']; 
     //use like this to store in session using $_SESSION global variable 
     $_SESSION["firstname"] = $row["firstname"]; //firstname - column name in table 
     $_SESSION["lastname"] = $row["lastname"]; 
     $_SESSION["email"] = $row["email"]; 
    } 
    else 
    { 
     echo "User does not exist"; 
    } 
} 
?> 
+0

'$ _GET ['username'] =" 'でテストしてください。user_reg;' '";"; – colburton

+0

あなたのコードはSQLインジェクションの脆弱です –

+0

@Gyanはこのステップを実際に議論してくれますか? (mysqli_num_rows($ checkUser)> 0) –

-1

あなたはこのようなあなたのcode.Try何かで多くのミスをしました:

<?php include("header.php"); 
     include("classes/db.php"); 
     session_start(); 
     $username=""; 
     if(isset($_GET['username'])) 
     { 
      $user=$_GET['username']; 
      $q = "SELECT user_name FROM user_reg WHERE user_name='$user'"; 
      $res = mysqli_query($con, q); 
      if(mysqli_num_rows($res) > 0) 
      { 
       while ($row = mysqli_fetch_array($res, $con)) { 
        //Show what you want here.. 
       } 
      } 
      else 
      { 
       echo "User does nto exist"; 
      } 
     } 
?> 
+1

これは、答えよりもスポット・ザ・ゲームのようなものです。あなたは何を変えましたか?なぜそれが問題を解決すべきですか? – Quentin

+0

だから、あなた自身の答えを追加しますか? –

0

てみてください、このような何かをするために -

<?php 
//checking for GET variable 
if(isset($_GET['username'])) $user=$_GET['username']; 
else exit("Username is missing"); 

//IGNORE this if your DB connection is set from other files 
//If so make a connection call and get the connection object. 
$servername = "localhost"; //your db servername 
$username = "username"; //your db username 
$password = "password"; //your db password 
$dbname = "myDB"; //your db name 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

//Your Query 
$sql = "SELECT user_name FROM user_reg WHERE user_name='$user'"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // found a row with the user entry. 
    // then the username exists in the database. 
    echo "Valid User"; 

} else { 
    echo "User not found"; 
} 
$conn->close(); 
?> 
+1

これは、答えよりもスポット・ザ・ゲームのようなものです。あなたは何を変えましたか?なぜそれが問題を解決すべきですか? – Quentin

+0

私はタイプしている間に私は他人の答えを見ていませんでした。私の悪い。 :p –

0
<?php 
$username=""; 
if(isset($_GET['username'])) 
{ 
$user=$_GET['username']; 
$checkUser = mysqli_query($con,"SELECT user_name FROM user_reg WHERE 
user_name='$user'") or die(mysqli_error($con)); 
$row=mysqli_fetch_assoc($checkUser); 
if(mysqli_num_rows($checkUser)>0){ 
    $username = $row["user_name"]; 
}else{ 
    echo "User does not exist"; 
} 
} 
?> 
+1

これは、答えよりもスポット・ザ・ゲームのようなものです。あなたは何を変えましたか?なぜそれが問題を解決すべきですか? – Quentin

関連する問題