2017-01-25 4 views
0

それは選択保ち、一つのレコードは、常に1つのレコードがある場合、それはあなたがいるは常に選択未選択の一つのレコードを残し、SQL

database image browser result image

+2

[Little Bobby](http://bobby-tables.com/)は*** [あなたのスクリプトはSQLインジェクション攻撃の危険にさらされていると言います。](http://stackoverflow.com/questions/60174/how-can- i-prevent-sql-injection-in-php)*** [MySQLi](http://php.net/manual)の[prepared](http://en.wikipedia.org/wiki/Prepared_statement)ステートメントについて学びます。 /en/mysqli.quickstart.prepared-statements.php)。 [文字列をエスケープする](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)でも安全ではありません! [それを信じていない?](http://stackoverflow.com/q/38297105/1011527) –

答えて

2

を表示しません

<?php 
require 'database.php'; 
//get from url 
if (isset($_GET['location'])) { 
    $location = $_GET['location']; 
} 
$sql = "SELECT * FROM bmen where location='$location'"; 
$result = mysqli_query($database,$sql) or die(mysqli_error()); 
$rws = mysqli_fetch_array($result); 

while($rws = $result->fetch_array()) 
{ 
echo $rws['username'] . " " . $rws['phone']; 
echo "<br />"; 
} 
$database->close(); 

を残していますあなたのループに入る前に、結果セットから最初の行を読み、それを放棄してください。

<?php 
require 'database.php'; 
//get from url 
if (isset($_GET['location'])) { 
    $location = $_GET['location']; 
} 
$sql = "SELECT * FROM bmen where location='$location'"; 
$result = mysqli_query($database,$sql) or die(mysqli_error()); 

// delete this next line 
//$rws = mysqli_fetch_array($result); 

while($rws = $result->fetch_array()) 
{ 
    echo $rws['username'] . " " . $rws['phone']; 
    echo "<br />"; 
} 
$database->close(); 

あなたのスクリプトがSQL Injection Attack する危険性があるとしてもLittle Bobby Tablesに何が起こったのかを見てください if you are escaping inputs, its not safe! 使用prepared parameterized statements

あなたはパラメータ化し、バインドされたデータを外部から収集したデータを使用してクエリをコーディング試してみてください

<?php 
require 'database.php'; 
//get from url 
if (isset($_GET['location'])) { 

    $sql = "SELECT * FROM bmen where location=?"; 
    $result = $database->prepare($sql); 
    $result->bind_param('s', $_GET['location']); 
    $result->execute(); 

    // some error checking is also good 
    if (! $result) { 
     echo $result->error; 
     exit; 
    }  

    while($rws = $result->fetch_assoc()) 
    { 
     echo $rws['username'] . " " . $rws['phone'] . "<br />"; 
    } 
} 
+0

ありがとうございます。 @ RiggsFolly。 – Kiwagi

0

これはmysqli_fetch_arrayを2回呼び出したためです。この理由から、最初の要素は常に除外されています。最初に削除します$ rws = mysqli_fetch_array($ result);。大丈夫だよ。

あなたがデータをフェッチするコードの中で
<?php 
require 'database.php'; 
//get from url 
if (isset($_GET['location'])) { 
    $location = $_GET['location']; 
} 
$sql = "SELECT * FROM bmen where location='$location'"; 
$result = mysqli_query($database,$sql) or die(mysqli_error()); 

while($rws = $result->fetch_array()) 
{ 
echo $rws['username'] . " " . $rws['phone']; 
echo "<br />"; 
} 
$database->close(); 
+0

ありがとう@reza – Kiwagi

0

2回

$rws = mysqli_fetch_array($result); 

と第二ここで最初の1

while($rws = $result->fetch_array()) 

のコード行を削除するには、$rws = mysqli_fetch_array($result);

罰金になります
+0

ありがとう@developersaumya私はそれを持っています – Kiwagi

関連する問題