1

我正在处理一个包含 19 个不同下拉列表的表单。每个列表都有相同的 39 个选项。我希望用户能够在一个或多个列表中选择选项并忽略其他选项。理想情况下,查询将返回 MySQL 数据库中与数据库中相应列中的所有选定选项匹配的行。

前任:

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>
4

1 回答 1

2

问题来自您的下拉菜单的默认值,以及对发布数组中值的测试。您还可以通过使用循环而不是$_POST单独测试数组的每个条目来显着缩短代码。

定义('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; ?>
于 2013-03-24T11:31:59.660 回答