PHP/MySQL: query based on selected form option

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JDJones

    PHP/MySQL: query based on selected form option

    Using PHP and MySQL. Trying to put a list of categories into a drop down
    select option of a form like:

    <form name="form" action="<? print $_SERVER['PHP_SELF']?>" method="get">
    <select name="subject">
    <option value=""></option>
    <option value="field1"> Field 1</option>
    <option value="field2"> Field 2</option>
    </select>

    <input type="submit" name="Submit" />
    </form>

    Then I want to process it so the MySQL query gets done depending on what
    was selected. I came up with this:

    //connect to database
    mysql_connect(" $dbhost","$dbus er","$dbpass" );
    mysql_select_db ("$dbase") or die("Unable to select database");

    // Build SQL Query
    if (isset($_GET['subject']))
    {
    switch($_GET['subject'])
    {
    case 'field1':
    $query = "select * from tips where category = 'field1'";
    break;
    case 'field2':
    $query = "select * from tips where text like 'field2' ";
    break;
    default:
    echo 'No subject found';
    }
    }

    $results=mysql_ query($query);
    $numrows=mysql_ num_rows($resul ts);

    etc etc etc

    But that switch doesn't seem to work. Anyone have a suggestion as to how
    I can code this to do the MySQL query based on the subject?

  • Pedro Graca

    #2
    Re: PHP/MySQL: query based on selected form option

    JDJones wrote:
    (snip)[color=blue]
    > Then I want to process it so the MySQL query gets done depending on what
    > was selected. I came up with this:
    >
    > //connect to database
    > mysql_connect(" $dbhost","$dbus er","$dbpass" );
    > mysql_select_db ("$dbase") or die("Unable to select database");
    >
    > // Build SQL Query
    > if (isset($_GET['subject']))
    > {
    > switch($_GET['subject'])
    > {
    > case 'field1':
    > $query = "select * from tips where category = 'field1'";
    > break;
    > case 'field2':
    > $query = "select * from tips where text like 'field2' ";
    > break;
    > default:
    > echo 'No subject found';[/color]

    ## set $query to false if no category found
    $query = false;
    [color=blue]
    > }
    > }
    >[/color]

    ## if there was a category entered
    if ($query) {
    [color=blue]
    > $results=mysql_ query($query);
    > $numrows=mysql_ num_rows($resul ts);
    >
    > etc etc etc[/color]

    ## end if
    }
    [color=blue]
    >
    > But that switch doesn't seem to work. Anyone have a suggestion as to how
    > I can code this to do the MySQL query based on the subject?[/color]

    Try the three lines I inserted in your code above.


    Also indent your code (even small scripts like this one!) so that you
    can much more easily catch eventual errors.


    compare with

    foreach ($arr as $k=>$v) foreach ($arr as $k=>$v)
    { {
    if ($k = 'name') if ($k = 'name')
    { {
    $b = '<b>' . $b . '</b>'; $b = '<b>' . $b . '</b>';
    $v = '<b>' . $v . '</b>'; $v = '<b>' . $v . '</b>';
    } }
    echo "$k = $v<br/>\n"; echo "$k = $v<br/>\n"
    } }
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--

    Comment

    • JDJones

      #3
      Re: PHP/MySQL: query based on selected form option

      Pedro Graca wrote:[color=blue]
      > JDJones wrote:
      > (snip)
      >[color=green]
      >>Then I want to process it so the MySQL query gets done depending on what
      >>was selected. I came up with this:
      >>
      >>//connect to database
      >>mysql_connect ("$dbhost","$db user","$dbpass" );
      >>mysql_select_ db("$dbase") or die("Unable to select database");
      >>
      >>// Build SQL Query
      >>if (isset($_GET['subject']))
      >>{
      >>switch($_GE T['subject'])
      >>{
      >>case 'field1':
      >>$query = "select * from tips where category = 'field1'";
      >>break;
      >>case 'field2':
      >>$query = "select * from tips where text like 'field2' ";
      >>break;
      >>default:
      >>echo 'No subject found';[/color]
      >
      >
      > ## set $query to false if no category found
      > $query = false;
      >
      >[color=green]
      >>}
      >>}
      >>[/color]
      >
      >
      > ## if there was a category entered
      > if ($query) {
      >
      >[color=green]
      >>$results=mysq l_query($query) ;
      >>$numrows=mysq l_num_rows($res ults);
      >>
      >>etc etc etc[/color]
      >
      >
      > ## end if
      > }
      >
      >[color=green]
      >>But that switch doesn't seem to work. Anyone have a suggestion as to how
      >>I can code this to do the MySQL query based on the subject?[/color]
      >
      >
      > Try the three lines I inserted in your code above.[/color]

      Thank you Pedro. That worked but you knew that, right? ;)

      Comment

      Working...