Jump to content

Recommended Posts

Hiya,

I am new to php , mysql so kindly be nice :)

 

My problem -> Created a dynamic website.

 

When i put this bit of code , it allows me to search both location and genre BUT if i select only location and nothing  from genre then it does not work . same for if i select genre only , it not work. So i have to select both from location and genre for it to work.

 

$result = mysql_query("SELECT * FROM bands WHERE  location in ('$location') AND genre in ('$genre')")
or die(mysql_error());  

 

Now , if i change AND to OR , i am able to get result with only selecting location or genre but nothing when select from both. I tried mixing and / or , played with brackets etc but no good. Been stuck on this almost 2 days.

 

Please can anyone help me here ?

 

I just need the search to be like , the user may select one / multiple from location + one/multiple from genre OR one/multiple from location only or one/multiple from genre only .....any permutaion combination should work.

 

if that makes sense  :(

 

cheers.

Link to comment
https://forums.phpfreaks.com/topic/208042-strange-problem-please-help/
Share on other sites

THAT error is because you are not passing an array to a function that expects and array. That has nothing to do with your mysql query question.

 

Why cannot people provide actual information that they know about a problem when they ask a for help :psychic:

 

No one can really help you with what is wrong with your code without seeing the actual code responsible for the symptom and the symptom.

<?php  

$location = implode("', '",$_POST['location']);

$genre = implode("', '",$_POST['genre']);

 

 

// Make a MySQL Connection

mysql_connect("localhost", "username", "password") or die(mysql_error());

mysql_select_db("db") or die(mysql_error());

 

 

$result = mysql_query("SELECT * FROM bands WHERE  (location in ('$location') AND genre in ('$genre'))

                                              OR (location in ('$location') OR genre in ('$genre'))

 

 

")

or die(mysql_error()); 

 

echo "<table border='5'>";

echo "<tr>

         

<th>Day</th>

                <th>Genre</th>

<th>Location</th>

<th>Name</th>

 

      </tr>";

 

// keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {

// Print out the contents of each row into a table

echo "<tr><td>";

echo $row['Day'];

echo "</td><td>";

echo $row['Genre'];

echo "</td><td>";

echo $row['Location'];

echo "</td><td>";

echo $row['Name'];

echo "</td></tr>";

 

}

 

 

echo "</table>";

 

 

?>

Thats the form code.

 

<form id="form1" method="post" action="advanced.php">

            <p>

              <label>Location

                <select name="location[]" size="3" multiple="multiple" id="location">

                  <option value="Old Trafford">Old Trafford</option>

                  <option value="Manchester Arena">Manchester Arena</option>

                    </select>

                  </label>

              <label> Genre

                <select name="genre[]" size="3" multiple="multiple" id="genre">

                  <option value="Pop">Pop</option>

                  <option value="Rock">Rock</option>

                    </select>

                  </label>

            </p>

    <p>

      <input type="submit" name="go" id="go" value="Submit" />

  </p>

  <p> </p>

              </form>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.