mysql error: You have an error in your SQL syntax;

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jaymzlx
    New Member
    • Feb 2010
    • 4

    mysql error: You have an error in your SQL syntax;

    My old problem is solved, but im running into a new error. I figured I would start a new topic, sorry if this was incorrect to do.

    mysql error: 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 'AND A.ID = P.ID' at line 4

    Code:
    <?php
    /* Display a vote form. */
    require_once("vote_config.php");
    $poll = $_GET['poll'];
    
    /*if (!is_numeric($poll)) 
      {
        die("Invalid poll");
      }*/
    
    /* Look up the poll in the database. */
    $sql = "SELECT P.question, A.answer, A.answer_ID
              FROM poll P, answer A
             WHERE P.ID = $poll
               AND A.ID = P.ID";
    
    $result = mysql_query($sql, $db) or die ("mysql error: " . mysql_error());
    
    if (mysql_num_rows($result) == 0) 
      {
        die('Invalid poll.');
      }
    
    /* If the user has already voted, show the results. */
    if ($_COOKIE["poll_voted_$poll"]) {
        header("Location: vote_tally.php?poll=$poll");
        exit;
    }
    
    /* Vote form */
    $question_list = "";
    while($row = mysql_fetch_array($result)) {
        $question = $row['question'];
        $question_list .= '<li><input name="answer" type="radio" value="' .
                          $row['answer_ID'] . '"> ' . $row['answer'] .
                          '</li>';
    }
    ?>
    <html>
    <head></head>
    <body>
    <span style="font-size: 12px;">
    <span style="font-weight: bold; font-size: 14px;">
        Poll #<?php print $poll; ?>
    </span><br />
    <span style="font-weight: bold"><?php print $question; ?></span>
    <form action="vote_process.php" method="post">
    <ul style="list-style-type: none;">
    <?php print $question_list; ?>
    </ul>
    <input name="poll" type="hidden" value="<?php print $poll; ?>">
    <input name="" type="submit" value="Vote!">
    </form>
    </span>
    </body></html
    The part I commented out:
    Code:
    /*if (!is_numeric($poll)) 
      {
        die("Invalid poll");
      }*/
    Kept returning invalid poll, even though it is not invalid, and the table is populated. How do I get rid of these errors? Thanks in advance.

    EDIT: on line 14 I put ' ' around $poll and commented out:

    Code:
    /*if (!is_numeric($poll)) 
      {
        die("Invalid poll");
      }*/
    and

    Code:
    /*if (mysql_num_rows($result) == 0) 
      {
        die("Invalid poll.");
      }*/
    the code runs, but the poll isnt populated. You can check it out at http://www.ruckusinc.net/projects/vote_form.php
    Last edited by Atli; Feb 18 '10, 11:03 AM. Reason: Added [code] tags around the last few snippets.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    The part you commented out is there to validate the $_GET['poll'] value. If it keeps telling you it is invalid, odds are that it is invalid. Commenting the validation code out won't make the input valid :)

    Try un-commenting that and adding a var_dump() before the die().
    [code=php]if (!is_numeric($p oll))
    {
    var_dump($poll) ;
    die("Invalid poll");
    }[/code]

    Also, when a SQL query fails and you can't spot the problem, it is always a good idea to print the query, just to see what is really going on.

    Comment

    • jaymzlx
      New Member
      • Feb 2010
      • 4

      #3
      I put in the var_dump($poll) ; and uncommented that section and it returned NULL.
      That's weird because I have information in that table I am trying to get

      EDIT: Its gotta be something with the line

      Code:
      $poll = $_GET['poll'];
      that is goofing up the code.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        That line simply fetches the poll variable from the URL.

        Your URL should be looking something like:
        - http://example.com/page.php?poll=1

        If you leave out the ?poll=1 part the $poll variable will be NULL and the die("Invalid poll"); statement is executed. - I assumed that was the reason you put it there; to let you know the poll ID was missing.

        Comment

        Working...