2012-03-17 22 views
1

特定のグループに属する特定の場所の在庫アイテムを検索するmysqlクエリがあります。したがって、これはwhile loopsの4つのレベルを通過しています。いいえ、私はユーザーに在庫を表示する場所を選択する権限を与えました。これは、配列内のajaxを使用して送信されるチェックボックスを使用して達成されています。 $offices = explode(",", $locations);を使用する配列exploded in PHP。しかし、今私は私のmysqlクエリで選択された場所を使用したい。forループを使ったmysqlステートメント

$locationは、私は今、これは実現可能な私には、方法がわからないことにより、forループを使用することができ、ユーザーが選択した場所に基づいて場所を選択したいlocation1, location2, location3, location4

//selecting all locations using the statement below, however i want to select the locations that where selected by user. 

$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where 'wanted locations'); 
    while($row3 = mysql_fetch_array($sql4)) { 
     $curr_location = $row3[0]; 

     $sql3 = mysql_query("select Quantity from Stock_Management where Book_ID = '$curr_book' and Location_ID = '$curr_location'"); 
     while($row3 = mysql_fetch_array($sql3)) { 
      echo "<td>".$row3[0]."</td>"; 
     } 
    } 
    echo "</tr>"; 

の形態であり、それは私のSQLクエリです!

+0

質問は何ですか? – Rufinus

+0

をコードのコメント領域に挿入します。私はそのSQLクエリの場所をユーザーが選択した場所に基づいて選択したいと思う。 – Namit

+2

私はこのコードを少し怖がっています:D – jondinham

答えて

1
$locations = mysql_real_escape_string($locations); 
$locations = str_replace(",","','",$locations); 

$sql = "select OfficeID, OfficeTitle from Office WHERE location in ('$locations')"; 
-1

SELECT OfficeID, OfficeTitle FROM Office WHERE OfficeID IN ($locations); ??また

mysql_real_escape_stringを見上げると「concerns`の分離

+0

ここでmysql_real_escape_stringがどのように役立つのでしょうか? –

+0

あなたの解決策には役に立たないでしょう。しかし、それはあなたのアプリケーションを保護するのに役立ちます。あなたがそれなしであなたの質問を書いてはいけません。 – Straseus

+0

mysql_real_escape_stringがこのアプリケーションを保護する方法は? –

1
$offices = explode(",", $locations); 
$loc = implode("','", $offices); 

これは役立ちますが、これがためにmysqlのクエリを作成し、変数$loc

$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where OfficeTitle IN ('$loc')");

location1','location2',location3

を作成します

$sql4 = mysql_query("select OfficeID, OfficeTitle from Office where OfficeTitle IN ('location1','location2',location3')");これは現在の目的を解決します。