Jump to content

form processing help


digitalgod

Recommended Posts

hey guys,

I'm trying to figure out how I can process a form that has several elements but might not all be "filled" for instance a user can select to search by sexe and/or age and/or location

How can I make the query dynamic?

so far I have this

[code]
<?php
foreach($_POST as $k => $v)
switch ($k) {
         case 'sexe':
               if (trim(stripslashes($v)) != '')
                   $qtmp[] = "gender ='" . mysql_real_escape_string(trim(stripslashes(implode ("','", $v)))) . "'";
                break;
                       
               }
$query = "SELECT * FROM " . $prefix . "users WHERE " . implode(', ', $qtmp) . "";
$_SESSION['custom_browse'] = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());?>
[/code]

so how can I make it so that if other fields are filled it will add "AND" and the conditions that are required? And if the radio button Both is checked, then it doesn't need to add WHERE gender=...

oh and I'm also getting this notice and was wondering if it's a bad thing or not Notice: Array to string conversion
Link to comment
https://forums.phpfreaks.com/topic/16058-form-processing-help/
Share on other sites

nevermind I think I solved it by doing

[code]
<?php
$qtmp = array();
foreach($_POST as $k => $v)
          switch ($k) {
                    case 'sexe':
                        $v = implode ("','", $v);
if ($v != 'both') {
                            $qtmp[] = "WHERE gender ='" . mysql_real_escape_string(trim(stripslashes($v))) . "' AND ";
} else {
    $qtmp[] = "WHERE ";
    }
                            break;
                       
        }
$query = "SELECT * FROM " . $prefix . "users " . implode(', ', $qtmp) . "level='2' ORDER by ID";
$_SESSION['custom_browse'] = mysql_query($query) or die("Problem with the query: $query<br>" . mysql_error());
?>
[/code]

now my only problem is to keep the radio button selected... so if I select Male then after I submit the form, Male is still checked.. I think I know how to do it but it might be messy
Link to comment
https://forums.phpfreaks.com/topic/16058-form-processing-help/#findComment-66176
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.