www.webdeveloper.com
Results 1 to 4 of 4

Thread: SQL syntax error

  1. #1
    Join Date
    Jan 2011
    Posts
    5

    SQL syntax error

    At a loss here... trying to sort database records using a drop down list but get an error message saying

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '2012'' at line 1

    Code:
     <?php 
    include "xxx.php";
    
    $sql = "SELECT * FROM players";
    
    if (isset ($_POST['search'])) {  
    
    $search_term = mysql_real_escape_string($_POST['class_filter']);
    
    
    $sql .= "WHERE class_filter = '{$search_term}'";
    }
    
    $query = mysql_query($sql) or die(mysql_error());
    
     ?>
    <form name="search_form" method="post" action="roster_search.php">
    <select name="class_filter" size="1">
      <option value="2012">2012</option>
      <option value="2011">2011</option>
    </select>
    <input type="submit" name="search" value="Filter by Class" />
    </form>
    <table>
    <tr>
    <th>
    Name
    </th>
    <th>
    Number
    </th>
    <th>
    Position
    </th>
    <th>
    Team
    </th>
    <th>
    Class
    </th>
    </tr>
    
    <?php while ($row = mysql_fetch_array($query)) { ?>.
    <tr>
    <td>
    <?php echo $row['name']; ?>
    </td>
    <td>
    <?php echo $row['number']; ?>
    </td>
    <td>
    <?php echo $row['position']; ?>
    </td>
    <td>
    <?php echo $row['team']; ?>
    </td>
    <td>
    <?php echo $row['class']; ?>
    </td>
    </tr>
    <?php } ?>
    
    </table>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,248
    You need some kind of white-space before "WHERE..."
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,248
    This would probably have made it obvious to you:
    PHP Code:
    $query mysql_query($sql) or die("<pre>".mysql_error()."\n".$sql."</pre>"); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Please check the xxx.php that you have included. Because there is error in line 1 then in line 1 you have included the xxx.php. May be this is the connection file so check if the connection is proper or not.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles