Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am working on a form that has 19 different drop down lists. Each list has the same 39 options in it. I'd like users to be able to select options in one or more of the lists and ignore the others. Ideally, the query would return rows from the MySQL database that match ALL the selected options in the corresponding columns in the database.

Ex:

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

When they hit submit, they would receive all words in the database that have BOTH the phoneme AA in the first position AND the phoneme CH in the third position. But I can't seem to figure out how to ignore the options they don't select. In the example, they left phonemes 2 and 4 blank. How can I have my query automatically only try to search for the ones they have chosen?

Thank you in advance for your help. The code I've been working with is below. Currently I get no results, although the database has over 130,000 words in it, each with their own phonetic makeup.

<?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:

<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>
share|improve this question
1  
Try to print $sql and check the sql string is as per your need or not by testing it on database – Arvind Mar 24 at 10:28
your operators are also != try !== and see if that makes a difference. (in the if-statements) – chris Mar 24 at 10:38

1 Answer

up vote 2 down vote accepted

The problem comes from the default value of your drop down, and the test of the value in the posted array. You may also shorten the code significantly by using a loop instead of testing each entry of the $_POST array individually.

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]);
}

Likewise, in the html part, change the default value of the drop down tags, and build using a loop:

 <?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; ?>
share|improve this answer
I implemented your suggestion absent generating the phon#'s in html (since they will always be the same). I'm still getting the same error I did before: Warning: mysql_numrows(): supplied argument is not a valid MySQL result resource in /home/content/83/9397583/html/phd/search3.php on line 43 – Gregory Schneider Mar 24 at 18:05
The code in the search.php file now reads as below. Line 43 is the $num = mysql_numrows($result); $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]); } } //-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(); ?> – Gregory Schneider Mar 24 at 18:11
Sorry, and I also did define PHONEME_COUNT just above with your suggested code. – Gregory Schneider Mar 24 at 18:12
it looks like there's a space missing before LIMIT in your query. Is this a typo or am I mistaken? – didierc Mar 24 at 18:33
I took out the LIMIT aspect just to see if it would work without it. Still got the same error. – Gregory Schneider Mar 24 at 19:19
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.