2017-04-06 47 views
0

我一直在尋找一個選擇框,可用於多選,以確定我想在我的research中查找哪個位置,並附帶此附件database。所以,在這裏了,這就是我發現要經過這樣的:如何將所選值從選擇框傳遞給研究

我把一個選擇框與我的幾個地點:

<select name="SelectLocation[]" MULTIPLE="yes"> 
<option value="1">Inbound</option> 
<option value="2">OutBound</option> 
<option value="3">Training Room</option> 
<option value="4">Summerside</option> 
<option value="5">Alberton</option> 
</select> 

我使用foreach給我選擇的位置:

foreach ($_GET['SelectLocation'] as $selectedOption){ 
//echo $selectedOption."\n"; 

$query1="select * from location where location_id=$selectedOption"; 
$info=info_query($query1); 
$nomLocation=$info['location']; 
$Location .= $nomLocation."\n"; 
} 
echo $Location; 

現在,如果我選擇Inbound和Summerside,回聲給我: 入站Summerside 這在同一行。

我使用查詢來獲取數據庫中的名稱(query1)。 我想現在使用這些「名稱」,去我選擇我的例子(入站和Summerside),做我的搜索,它只採取選定的位置,而不是另一個,所以我在我的查詢中做到這一點:

location.location like".$Location; 
$query="select computer.computer_id, 
computer.computer_name, 
computer.product_key, 
computer.model, 
computer.serial_number, 
`status`.`status`, 
computer.starphone, 
computer.inst_id, 
computer.did, 
computer.macaddress, 
software.description, 
vendor.vendor_name, 
location.location, 
department.department, 
jack.jack_number 
from computer 
inner join computer_vendor on computer.computer_id=computer_vendor.computer_id 
inner join vendor on computer_vendor.vendor_id=vendor.vendor_id 
inner join `status`on computer.status_id=`status`.status_id 
inner join software on computer.software_id=software.software_id 
inner join jack on jack.computer_id=computer.computer_id 
inner join location on location.location_id=jack.location_id 
inner join department on department.dept_id=jack.dept_id 
where computer.computer_name like '%".$critere."%' and location.location like".$Location; 

我的問題是:我對我的例子Inbound和Summerside做了一個研究,它沒有給我任何記錄。 這是因爲我的位置可以在數據庫中找到?我試圖把%之前和之後$Location$critere,但沒有幫助。我是否需要使用價值和location_id,如果它不與該位置的名稱?

我想要的是,我的$Location能夠告訴數據庫選擇的位置,並使選擇與數據庫完美匹配,我的研究顯示了所選位置和標準所需的所有信息。

如果需要更多信息,請告訴我,我會提供。

select computer.computer_id, 
computer.computer_name, 
computer.product_key, 
computer.model, 
computer.serial_number, 
`status`.`status`, 
computer.starphone, 
computer.inst_id, 
computer.did, 
computer.macaddress, 
software.description, 
vendor.vendor_name, 
location.location, 
department.department, 
jack.jack_number from computer inner join computer_vendor on computer.computer_id=computer_vendor.computer_id 
inner join vendor on computer_vendor.vendor_id=vendor.vendor_id 
inner join `status`on computer.status_id=`status`.status_id 
inner join software on computer.software_id=software.software_id 
inner join jack on jack.computer_id=computer.computer_id 
inner join location on location.location_id=jack.location_id 
inner join department on department.dept_id=jack.dept_id 
where computer.computer_name like '%52%' and location.location in() 
+0

您的代碼很容易受到SQL注入式攻擊。您應該使用[mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)或[PDO](http://php.net/manual/en/pdo.prepared- statement.php)按照[本文]中描述的準備語句(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)。 –

+0

因爲你有一個位置數組,你可能想[閱讀此](http://stackoverflow.com/questions/907806/passing-an-array-to-a-query-using-a-where-clause)讓mysql做這個工作? – OldPadawan

+0

mysqli它已經在另一個網頁上調用cnx_db。 部分是,我會去讀 – Nexis

回答

0
$Location=array(); 
foreach ($_GET['SelectLocation'] as $selectedOption){ 
//echo $selectedOption."\n"; 

$query1="select * from location where location_id=$selectedOption"; 
$info=info_query($query1); 
$nomLocation=$info['location']; 
$Location[] = addslashes($nomLocation); 
} 
$locationConsolidated = "'".join("','", $Location)."'"; 

,並使用該$ locationConsolidated爲...

where computer.computer_name like '%".$critere."%' and location.location in ($locationConsolidated) 
+0

這正是我需要的,我的問題是:搜索無法找到他們想要的一些記錄! – Nexis

+0

那麼這是否解決你的問題? –

+0

是的,但現在,他沒有給我我的數據庫中的任何記錄,只是告訴我:沒有記錄(當數據什麼也沒有給出信息) – Nexis

相關問題