2013-03-24 57 views
1

我正在處理一個有19個不同下拉列表的表單。每個列表中都有相同的39個選項。我希望用戶能夠在一個或多個列表中選擇選項並忽略其他選項。理想情況下,查詢將從MySQL數據庫返回匹配數據庫中相應列中所有選定選項的行。忽略PHP/MySQL中multipe下拉列表的空值

例:

Phoneme 1: AA <--- choice in a dropdown list of 39 items

Phoneme 2: <--- choice in a dropdown list of 39 items

Phoneme 3: CH <--- choice in a dropdown list of 39 items

Phoneme 4: <--- choice in a dropdown list of 39 items

當他們點擊提交,他們將獲得在既有的音素AA數據庫中的所有單詞第一個位置和音素CH進來第三個位置。但我似乎無法弄清楚如何忽略他們不選擇的選項。在這個例子中,他們將音素2和4留下空白。我怎樣才能自動查詢我的查詢只搜索他們選擇的查詢?

非常感謝您的幫助。我一直在使用的代碼如下。目前我沒有得到任何結果,儘管數據庫中有超過130,000個單詞,每個單詞都有自己的語音化妝。

<?php 

include 'db_connect.php'; 

//submitted form variable definitions 
$phon1=$_POST['phon1']; 
$phon2=$_POST['phon2']; 
$phon3=$_POST['phon3']; 
$phon4=$_POST['phon4']; 
$phon5=$_POST['phon5']; 
$phon6=$_POST['phon6']; 
$phon7=$_POST['phon7']; 
$phon8=$_POST['phon8']; 
$phon9=$_POST['phon9']; 
$phon10=$_POST['phon10']; 
$phon11=$_POST['phon11']; 
$phon12=$_POST['phon12']; 
$phon13=$_POST['phon13']; 
$phon14=$_POST['phon14']; 
$phon15=$_POST['phon15']; 
$phon16=$_POST['phon16']; 
$phon17=$_POST['phon17']; 
$phon18=$_POST['phon18']; 
$phon19=$_POST['phon19']; 

//-query the database table 

$sql = ''; 
if ($phon1!=0) { 
    $sql .= ' AND phon1 LIKE ' . $phon1; 
} 

if ($phon2!=0) { 
    $sql .= ' AND phon2 LIKE ' . $phon2; 
} 

if ($phon3!=0) { 
    $sql .= ' AND phon3 LIKE ' . $phon3; 
} 

if ($phon4!=0) { 
    $sql .= ' AND phon4 LIKE ' . $phon4; 
} 

if ($phon5!=0) { 
    $sql .= ' AND phon5 LIKE ' . $phon5; 
} 

if ($phon6!=0) { 
    $sql .= ' AND phon6 LIKE ' . $phon6; 
} 

if ($phon7!=0) { 
    $sql .= ' AND phon7 LIKE ' . $phon7; 
} 

if ($phon8!=0) { 
    $sql .= ' AND phon8 LIKE ' . $phon8; 
} 

if ($phon9!=0) { 
    $sql .= ' AND phon9 LIKE ' . $phon9; 
} 

if ($phon10!=0) { 
    $sql .= ' AND phon10 LIKE ' . $phon10; 
} 

if ($phon11!=0) { 
    $sql .= ' AND phon11 LIKE ' . $phon11; 
} 

if ($phon12!=0) { 
    $sql .= ' AND phon12 LIKE ' . $phon12; 
} 

if ($phon13!=0) { 
    $sql .= ' AND phon13 LIKE ' . $phon13; 
} 

if ($phon14!=0) { 
    $sql .= ' AND phon14 LIKE ' . $phon14; 
} 

if ($phon15!=0) { 
    $sql .= ' AND phon15 LIKE ' . $phon15; 
} 

if ($phon16!=0) { 
    $sql .= ' AND phon16 LIKE ' . $phon16; 
} 

if ($phon17!=0) { 
    $sql .= ' AND phon17 LIKE ' . $phon17; 
} 

if ($phon18!=0) { 
    $sql .= ' AND phon18 LIKE ' . $phon18; 
} 

if ($phon19!=0) { 
    $sql .= ' AND phon19 LIKE ' . $phon19; 
} 


//-run the query against the mysql query function 
$result = mysql_query('SELECT * FROM bigdic WHERE 1=1' . $sql . 'LIMIT 0,100'); 
$num = mysql_numrows($result); 

mysql_close(); 
?> 

<b>Search Results:</b> 
<table border="1" cellspacing="2" cellpadding="2"> 
    <tr> 
     <th>Word</th> 
     <th>Ph1</th> 
     <th>Ph2</th> 
     <th>Ph3</th> 
     <th>Ph4</th> 
     <th>Ph5</th> 
     <th>Ph6</th> 
     <th>Ph7</th> 
     <th>Ph8</th> 
     <th>Ph9</th> 
     <th>Ph10</th> 
     <th>Ph11</th> 
     <th>Ph12</th> 
     <th>Ph13</th> 
     <th>Ph14</th> 
     <th>Ph15</th> 
     <th>Ph16</th> 
     <th>Ph17</th> 
     <th>Ph18</th> 
     <th>Ph19</th> 
    </tr> 

<? 
//setting up the loop 
$i=0; 
while($i<$num) { 

    $word=mysql_result($result,$i,"word"); 
    $ph1=mysql_result($result,$i,"ph1"); 
    $ph2=mysql_result($result,$i,"ph2"); 
    $ph3=mysql_result($result,$i,"ph3"); 
    $ph4=mysql_result($result,$i,"ph4"); 
    $ph5=mysql_result($result,$i,"ph5"); 
    $ph6=mysql_result($result,$i,"ph6"); 
    $ph7=mysql_result($result,$i,"ph7"); 
    $ph8=mysql_result($result,$i,"ph8"); 
    $ph9=mysql_result($result,$i,"ph9"); 
    $ph10=mysql_result($result,$i,"ph10"); 
    $ph11=mysql_result($result,$i,"ph11"); 
    $ph12=mysql_result($result,$i,"ph12"); 
    $ph13=mysql_result($result,$i,"ph13"); 
    $ph14=mysql_result($result,$i,"ph14"); 
    $ph15=mysql_result($result,$i,"ph15"); 
    $ph16=mysql_result($result,$i,"ph16"); 
    $ph17=mysql_result($result,$i,"ph17"); 
    $ph18=mysql_result($result,$i,"ph18"); 
    $ph19=mysql_result($result,$i,"ph19"); 
?> 

    <tr> 
    <td><B><? echo $word; ?></B></td> 
    <td><? echo $ph1; ?></td> 
    <td><? echo $ph2; ?></td> 
    <td><? echo $ph3; ?></td> 
    <td><? echo $ph4; ?></td> 
    <td><? echo $ph5; ?></td> 
    <td><? echo $ph6; ?></td> 
    <td><? echo $ph7; ?></td> 
    <td><? echo $ph8; ?></td> 
    <td><? echo $ph9; ?></td> 
    <td><? echo $ph10; ?></td> 
    <td><? echo $ph11; ?></td> 
    <td><? echo $ph12; ?></td> 
    <td><? echo $ph13; ?></td> 
    <td><? echo $ph14; ?></td> 
    <td><? echo $ph15; ?></td> 
    <td><? echo $ph16; ?></td> 
    <td><? echo $ph17; ?></td> 
    <td><? echo $ph18; ?></td> 
    <td><? echo $ph19; ?></td> 
    </tr> 

<? 
    $i++; 
} 

echo "</table>"; 

?> 

<b><? echo $num; ?></b> records returned.` 

HTML表單:

<form method="post" action="search2.php?go" id="searchform2"> 
    Phoneme 01: <select name="phon1"> 
     <option value="0" selected></option> 
     <option value="AA">AA</option> 
     <option value="AE">AE</option> 
     <option value="AH">AH</option> 
     <option value="AO">AO</option> 
     <option value="AW">AW</option> 
     <option value="AY">AY</option> 
     <option value="B">B</option> 
     <option value="CH">CH</option> 
     <option value="D">D</option> 
     <option value="DH">DH</option> 
     <option value="EH">EH</option> 
     <option value="ER">ER</option> 
     <option value="EY">EY</option> 
     <option value="F">F</option> 
     <option value="G">G</option> 
     <option value="HH">HH</option> 
     <option value="IH">IH</option> 
     <option value="IY">IY</option> 
     <option value="JH">JH</option> 
     <option value="K">K</option> 
     <option value="L">L</option> 
     <option value="M">M</option> 
     <option value="N">N</option> 
     <option value="NG">NG</option> 
     <option value="OW">OW</option> 
     <option value="OY">OY</option> 
     <option value="P">P</option> 
     <option value="R">R</option> 
     <option value="S">S</option> 
     <option value="SH">SH</option> 
     <option value="T">T</option> 
     <option value="TH">TH</option> 
     <option value="UH">UH</option> 
     <option value="UW">UW</option> 
     <option value="V">V</option> 
     <option value="W">W</option> 
     <option value="Y">Y</option> 
     <option value="Z">Z</option> 
     <option value="ZH">ZH</option> 
    </select><br \> 

    Phoneme 02: <select name="phon2"> 
     <option value="0" selected></option> 
     <option value="AA">AA</option> 
     <option value="AE">AE</option> 
     <option value="AH">AH</option> 
     <option value="AO">AO</option> 
     <option value="AW">AW</option> 
     <option value="AY">AY</option> 
     <option value="B">B</option> 
     <option value="CH">CH</option> 
     <option value="D">D</option> 
     <option value="DH">DH</option> 
     <option value="EH">EH</option> 
     <option value="ER">ER</option> 
     <option value="EY">EY</option> 
     <option value="F">F</option> 
     <option value="G">G</option> 
     <option value="HH">HH</option> 
     <option value="IH">IH</option> 
     <option value="IY">IY</option> 
     <option value="JH">JH</option> 
     <option value="K">K</option> 
     <option value="L">L</option> 
     <option value="M">M</option> 
     <option value="N">N</option> 
     <option value="NG">NG</option> 
     <option value="OW">OW</option> 
     <option value="OY">OY</option> 
     <option value="P">P</option> 
     <option value="R">R</option> 
     <option value="S">S</option> 
     <option value="SH">SH</option> 
     <option value="T">T</option> 
     <option value="TH">TH</option> 
     <option value="UH">UH</option> 
     <option value="UW">UW</option> 
     <option value="V">V</option> 
     <option value="W">W</option> 
     <option value="Y">Y</option> 
     <option value="Z">Z</option> 
     <option value="ZH">ZH</option> 
    </select><br \> 

    Phoneme 03: <select name="phon3"> 
     <option value="0" selected></option> 
     <option value="AA">AA</option> 
     <option value="AE">AE</option> 
     <option value="AH">AH</option> 
     <option value="AO">AO</option> 
     <option value="AW">AW</option> 
     <option value="AY">AY</option> 
     <option value="B">B</option> 
     <option value="CH">CH</option> 
     <option value="D">D</option> 
     <option value="DH">DH</option> 
     <option value="EH">EH</option> 
     <option value="ER">ER</option> 
     <option value="EY">EY</option> 
     <option value="F">F</option> 
     <option value="G">G</option> 
     <option value="HH">HH</option> 
     <option value="IH">IH</option> 
     <option value="IY">IY</option> 
     <option value="JH">JH</option> 
     <option value="K">K</option> 
     <option value="L">L</option> 
     <option value="M">M</option> 
     <option value="N">N</option> 
     <option value="NG">NG</option> 
     <option value="OW">OW</option> 
     <option value="OY">OY</option> 
     <option value="P">P</option> 
     <option value="R">R</option> 
     <option value="S">S</option> 
     <option value="SH">SH</option> 
     <option value="T">T</option> 
     <option value="TH">TH</option> 
     <option value="UH">UH</option> 
     <option value="UW">UW</option> 
     <option value="V">V</option> 
     <option value="W">W</option> 
     <option value="Y">Y</option> 
     <option value="Z">Z</option> 
     <option value="ZH">ZH</option> 
    </select><br \>  

    <input type="submit" name="submit" value="Search"> 
</form> 
+1

嘗試打印$ SQL和檢查SQL字符串是按照您的需要或不通過在數據庫上測試 – Arvind 2013-03-24 10:28:04

+0

您的運營商也是'!='嘗試'!==',看看這是否有所作爲。 (在if語句中) – chris 2013-03-24 10:38:13

回答

2

的問題來自於您的下拉的默認值,並張貼數組中值的測試。您也可以使用循環來顯着縮短代碼,而不是單獨測試$_POST數組的每個條目。

define('PHONEME_COUNT',19);

$sql = ''; 
for ($i = 0; $i < PHONEME_COUNT; $i++){ 
    $opt = 'phon'.$i; 
    if (isset($_POST[$opt]) && $_POST[$opt]!='--') { 
     $sql .= ' AND `'.$opt.'` LIKE ' .mysql_real_escape_string($_POST[$opt]); 
} 

同樣,在HTML部分,將下拉選項更改標籤的默認值,並建立使用循環:

<?php for($i = 0; $i < PHONEME_COUNT; $I++): ?> 
Phoneme <?php echo $i; ?> : <select name="phon<?php echo $i;?>"> 
    <option value="--" selected></option> 
    <!-- options --> 
    </select> 


<?php endfor; ?> 
+0

我實現了你的建議,因爲沒有在html中生成phon#(因爲它們總是相同的)。我仍然得到了我以前做過的同樣的錯誤:警告:mysql_numrows():提供的參數不是在/home/content/83/9397583/html/phd/search3.php 43行上的有效MySQL結果資源 – 2013-03-24 18:05:20

+0

代碼在search.php文件中現在讀取如下。第43行是'$ num = mysql_numrows($ result);' $ sql =''; ($ i = 0; $ i 2013-03-24 18:11:37

+0

對不起,我也在上面用你的建議代碼定義了PHONEME_COUNT。 – 2013-03-24 18:12:45