???passing a query from a form???

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

    ???passing a query from a form???

    I have wrote a script to query my mysql database based up a name that
    is selected from an HTML list menu, the form action calls to my php
    script from my html doc, I have tested connectivity to the databse and
    have added a manual sql query to my php script to be sure it is
    functioning properly, the problem is I'm not sure how to get the
    script to see what option the html form is sending, I thought I could
    use:

    $query = "SELECT * FROM table WHERE email =
    '{$_GET['selecteduser']}@mydomain.com' ";

    but that doesn't seem to be working properly. Maybe the HTML document
    is sending it correctly? Here is my form:

    <form action="report_ results.php" method="post">
    <select name="selectedu ser">
    <option value="selected user">Select One:</option>
    <option value="user1@my domain.com">use r1</option>
    <option value="user2@my domain.com">use r2</option>
    </select>
    <input type="submit" name="submit" value="Go!">
    </form>

    This form calls to the php script which then takes the input from the
    HTML form and processes it into the $_GET query and sends it to the
    while loop in the script, I'm not sure why this is not working? Here
    is my php code:



    PHP:
    --------------------------------------------------------------------------------
    <?php
    $db = mysql_connect(" localhost", "sqluser", "sqlpasswd" ) or
    die("Could not connect to Mysql");
    if (!$db == False)
    {
    mysql_select_db ("mydb");
    $query = "SELECT * FROM table WHERE email =
    '{$_GET['selecteduser']}@mydomain.com' ";

    $result = mysql_query($sq l,$db);
    while ($row = mysql_fetch_row ($result))
    {
    print "<tr>";
    foreach ($row as $field)
    {
    print "<td>$field </td>";
    }
    print "</tr>";
    }
    mysql_close($cn );
    }
    else
    {
    print "Problem Connecting to the Database<br>";
    }
    ?>
    --------------------------------------------------------------------------------



    Does anyone have any suggestions??

    Thanks
  • Riddic

    #2
    Re: ???passing a query from a form???

    On 28 Mar 2004 17:11:54 -0800, t3463@hotmail.c om (Joe) wrote:
    [color=blue]
    >I have wrote a script to query my mysql database based up a name that
    >is selected from an HTML list menu, the form action calls to my php
    >script from my html doc, I have tested connectivity to the databse and
    >have added a manual sql query to my php script to be sure it is
    >functioning properly, the problem is I'm not sure how to get the
    >script to see what option the html form is sending, I thought I could
    >use:
    >
    >$query = "SELECT * FROM table WHERE email =
    >'{$_GET['selecteduser']}@mydomain.com' ";
    >
    >but that doesn't seem to be working properly. Maybe the HTML document
    >is sending it correctly? Here is my form:
    >
    ><form action="report_ results.php" method="post">
    ><select name="selectedu ser">
    ><option value="selected user">Select One:</option>
    ><option value="user1@my domain.com">use r1</option>
    ><option value="user2@my domain.com">use r2</option>
    ></select>
    ><input type="submit" name="submit" value="Go!">
    ></form>
    >
    >This form calls to the php script which then takes the input from the
    >HTML form and processes it into the $_GET query and sends it to the
    >while loop in the script, I'm not sure why this is not working? Here
    >is my php code:
    >
    >
    >
    >PHP:
    >--------------------------------------------------------------------------------
    > <?php
    > $db = mysql_connect(" localhost", "sqluser", "sqlpasswd" ) or
    >die("Could not connect to Mysql");
    > if (!$db == False)
    > {
    > mysql_select_db ("mydb");
    > $query = "SELECT * FROM table WHERE email =
    >'{$_GET['selecteduser']}@mydomain.com' ";
    >
    > $result = mysql_query($sq l,$db);
    > while ($row = mysql_fetch_row ($result))
    > {
    > print "<tr>";
    > foreach ($row as $field)
    > {
    > print "<td>$field </td>";
    > }
    > print "</tr>";
    > }
    > mysql_close($cn );
    > }
    > else
    > {
    > print "Problem Connecting to the Database<br>";
    > }
    >?>
    >--------------------------------------------------------------------------------
    >
    >
    >
    >Does anyone have any suggestions??
    >
    >Thanks[/color]



    Try
    $_POST['selecteduser']
    instead of
    $_GET['selecteduser']

    in the PHP script.

    Either that or use method="get" in your HTML page.

    That oughta do it :]

    Comment

    • Eric Bohlman

      #3
      Re: ???passing a query from a form???

      t3463@hotmail.c om (Joe) wrote in
      news:69fa4029.0 403281711.78587 118@posting.goo gle.com:
      [color=blue]
      > $query = "SELECT * FROM table WHERE email =
      > '{$_GET['selecteduser']}@mydomain.com' ";[/color]

      You're trying to retrieve the value from $_GET, which assumes your form was
      using the GET method...[color=blue]
      >
      > but that doesn't seem to be working properly. Maybe the HTML document
      > is sending it correctly? Here is my form:
      >
      > <form action="report_ results.php" method="post">[/color]

      but your form is using the POST method, so the value will be in $_POST.

      But wait, there's more...
      [color=blue]
      > <select name="selectedu ser">
      > <option value="selected user">Select One:</option>
      > <option value="user1@my domain.com">use r1</option>
      > <option value="user2@my domain.com">use r2</option>[/color]

      The value in $_POST will end in "@mydomain. com" so when you fix the first
      problem, you'll find you're asking your database for a value ending in
      "@mydomain.com@ mydomain.com" and it will tell you it doesn't have it.
      Append it in one place, not two.

      Oh, and I'd really run that value through mysql_escape_st ring() before
      letting it near a SELECT statement.

      Comment

      • Joe

        #4
        Re: ???passing a query from a form???

        I changed the method to post in my html document, so that it matches
        in both documents.I also the line <option value="selected user">Select
        One:</option>
        to <option value="NULL">Se lect One:</option> since there is no option
        to pass there.

        Now when you say:
        The value in $_POST will end in "@mydomain. com" so when you fix the
        first
        problem, you'll find you're asking your database for a value ending in
        "@mydomain.com@ mydomain.com" and it will tell you it doesn't have it.
        Append it in one place, not two.

        Do you mean only put the $_POST in my $sql query and no where else?



        Eric Bohlman <ebohlman@earth link.net> wrote in message news:<Xns94BADC C082D8Eebohlman omsdevcom@130.1 33.1.4>...[color=blue]
        > t3463@hotmail.c om (Joe) wrote in
        > news:69fa4029.0 403281711.78587 118@posting.goo gle.com:
        >[color=green]
        > > $query = "SELECT * FROM table WHERE email =
        > > '{$_GET['selecteduser']}@mydomain.com' ";[/color]
        >
        > You're trying to retrieve the value from $_GET, which assumes your form was
        > using the GET method...[color=green]
        > >
        > > but that doesn't seem to be working properly. Maybe the HTML document
        > > is sending it correctly? Here is my form:
        > >
        > > <form action="report_ results.php" method="post">[/color]
        >
        > but your form is using the POST method, so the value will be in $_POST.
        >
        > But wait, there's more...
        >[color=green]
        > > <select name="selectedu ser">
        > > <option value="selected user">Select One:</option>
        > > <option value="user1@my domain.com">use r1</option>
        > > <option value="user2@my domain.com">use r2</option>[/color]
        >
        > The value in $_POST will end in "@mydomain. com" so when you fix the first
        > problem, you'll find you're asking your database for a value ending in
        > "@mydomain.com@ mydomain.com" and it will tell you it doesn't have it.
        > Append it in one place, not two.
        >
        > Oh, and I'd really run that value through mysql_escape_st ring() before
        > letting it near a SELECT statement.[/color]

        Comment

        • Ruby Tuesdays

          #5
          Re: ???passing a query from a form???

          Perhaps you overlook this line:
          $result = mysql_query($sq l,$db);

          I think it should be
          $result = mysql_query($qu ery,$db);



          Comment

          Working...