Forum Moderators: coopster

Message Too Old, No Replies

form --> $sql

what's the proper way?

         

muszek

4:37 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



I'm making a little tool for my community. It lets users publish friendly games offers and search through them. That's a part of the form I'm using:

Fanclub minimum: <INPUT type="text" name="ht_fanclubsize" size="6">

Home/Away: <select name="friend_where">
<option value="0">Anywhere</option>
<option value="1">They will only play at home</option>
<option value="2">They will only play away</option>
</select>

After the form is submitted, the script needs to construct an sql query. So for example, if friend_where is 0, then it's not in the query at all.
If ht_fanclubsize is not set, it shouldn't be in the query either.
How do I properly do it?

greetings,
mateusz mucha

httpwebwitch

5:08 pm on Jul 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try this:


$query="SELECT * FROM mytable WHERE 1";
if (isset($_POST['fanclubsize'])){
$query.=" AND fanclubsize=".$_POST['fanclubsize'];
}
if ($_POST['friend_where']!=0){
$query.=" AND friend_where=".$_POST['friend_where'];
}
// debug
echo $query;

Note the use of "1" in the WHERE clause (which is always true), and the appended use of AND. That method makes it easier to build the query with proper syntax.

good luck!

muszek

7:32 pm on Jul 9, 2004 (gmt 0)

10+ Year Member



danke!