Run a SQL query based on HTML drop down selection?

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

    Run a SQL query based on HTML drop down selection?

    This is my HTML form:

    <form method=get action="home.ph p">
    <INPUT TYPE = "Text" VALUE = "" NAME = "title"><br >
    <select name="searchby" >
    <option value="Title">T itle</option>
    <option value="Year">Ye ar</option>
    <option value="Genre">G enre</option>
    <option value="Director ">Director</option>
    </select>

    And here is the relevant PHP query:

    $query = "SELECT Title, ReleaseYear, Rating, Director, RunningTime,
    Genre
    FROM movie
    WHERE $searchby LIKE '%$title%'
    GROUP BY $searchby";

    Assume that "$title" is a string of text (not necessarily a movie
    title). My problem seems to be that I cant use the variable $searchby
    within a SQL query. Is there a workaround for this?
  • Jerry Stuckle

    #2
    Re: Run a SQL query based on HTML drop down selection?

    Joe wrote:
    This is my HTML form:
    >
    <form method=get action="home.ph p">
    <INPUT TYPE = "Text" VALUE = "" NAME = "title"><br >
    <select name="searchby" >
    <option value="Title">T itle</option>
    <option value="Year">Ye ar</option>
    <option value="Genre">G enre</option>
    <option value="Director ">Director</option>
    </select>
    >
    And here is the relevant PHP query:
    >
    $query = "SELECT Title, ReleaseYear, Rating, Director, RunningTime,
    Genre
    FROM movie
    WHERE $searchby LIKE '%$title%'
    GROUP BY $searchby";
    >
    Assume that "$title" is a string of text (not necessarily a movie
    title). My problem seems to be that I cant use the variable $searchby
    within a SQL query. Is there a workaround for this?
    >
    There should be no problem - as long as your columns are named Title,
    Year, Genre and Director.

    What do you get when you echo $query, and what is your error message?

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • GazK

      #3
      Re: Run a SQL query based on HTML drop down selection?

      Jerry Stuckle wrote:
      Joe wrote:
      >This is my HTML form:
      >>
      ><form method=get action="home.ph p">
      ><INPUT TYPE = "Text" VALUE = "" NAME = "title"><br >
      ><select name="searchby" >
      ><option value="Title">T itle</option>
      ><option value="Year">Ye ar</option>
      ><option value="Genre">G enre</option>
      ><option value="Director ">Director</option>
      ></select>
      >>
      >And here is the relevant PHP query:
      >>
      >$query = "SELECT Title, ReleaseYear, Rating, Director, RunningTime,
      >Genre
      >FROM movie
      >WHERE $searchby LIKE '%$title%'
      >GROUP BY $searchby";
      >>
      >Assume that "$title" is a string of text (not necessarily a movie
      >title). My problem seems to be that I cant use the variable $searchby
      >within a SQL query. Is there a workaround for this?
      >>
      >
      There should be no problem - as long as your columns are named Title,
      Year, Genre and Director.
      >
      What do you get when you echo $query, and what is your error message?
      >
      A couple of possibilities to try:

      - does $searchby need to be in quotes? ie .... GROUP BY '$searchby'"
      - depending on your php setup, you may need to use $_GET['searchby']
      rather than $searchby - although this would affect your other form
      parameters too.

      Comment

      • C. (http://symcbean.blogspot.com/)

        #4
        Re: Run a SQL query based on HTML drop down selection?

        On 30 Oct, 07:34, GazK <inva...@invali d.invalidwrote:
        Jerry Stuckle wrote:
        Joe wrote:
        This is my HTML form:
        >
        <form method=get action="home.ph p">
        <INPUT TYPE = "Text" VALUE = "" NAME = "title"><br >
        <select name="searchby" >
        <option value="Title">T itle</option>
        <option value="Year">Ye ar</option>
        <option value="Genre">G enre</option>
        <option value="Director ">Director</option>
        </select>
        >
        And here is the relevant PHP query:
        >
        $query = "SELECT Title, ReleaseYear, Rating, Director, RunningTime,
        Genre
        FROM movie
        WHERE $searchby LIKE '%$title%'
        GROUP BY $searchby";
        >
        Assume that "$title" is a string of text (not necessarily a movie
        title). My problem seems to be that I cant use the variable $searchby
        within a SQL query. Is there a workaround for this?
        >
        There should be no problem - as long as your columns are named Title,
        Year, Genre and Director.
        >
        What do you get when you echo $query, and what is your error message?
        >
        A couple of possibilities to try:
        >
        - does $searchby need to be in quotes? ie .... GROUP BY '$searchby'"
        No. Its a column name.
        - depending on your php setup, you may need to use $_GET['searchby']
        rather than $searchby - although this would affect your other form
        parameters too.
        No - register_global s is deprecated - even if it is configured as 'on'
        you should write your code as if it were off.

        And, even though MySQL only allows one query per function call, there
        should be validation in place to prevent SQL injection.

        OP's code should work.

        C.

        Comment

        • musicishare_dotcom

          #5
          Re: Run a SQL query based on HTML drop down selection?

          On Oct 29, 8:55 pm, Joe <sutclif...@gma il.comwrote:
          This is my HTML form:
          >
          <form method=get action="home.ph p">
          <INPUT TYPE = "Text" VALUE = "" NAME = "title"><br >
          <select name="searchby" >
          <option value="Title">T itle</option>
          <option value="Year">Ye ar</option>
          <option value="Genre">G enre</option>
          <option value="Director ">Director</option>
          </select>
          >
          And here is the relevant PHP query:
          >
          $query = "SELECT Title, ReleaseYear, Rating, Director, RunningTime,
          Genre
          FROM movie
          WHERE $searchby LIKE '%$title%'
          GROUP BY $searchby";
          >
          Assume that "$title" is a string of text (not necessarily a movie
          title). My problem seems to be that I cant use the variable $searchby
          within a SQL query. Is there a workaround for this?
          The first thing I notice is that if You search by Year, the column
          name will not agree with the db table. A troubleshooting technique i
          use is to build the query in a variable, then echo out the
          variable(the query) to make sure the right query is being run.
          Hope this helps....

          Comment

          Working...