Error querying database

Discussion in 'PHP' started by dmwesq, Dec 8, 2010.

  1. #1
    Try as I may, I keep getting an error querying database message, and just can't figure it out. I have shortened my code considerably to try narrowing it down, but still no luck.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>Troop 97 Permission Form</title>
      <link rel="stylesheet" type="text/css" href="style.css" />
      <style>
      .legal {
      	font-size:11px;
    	font-style:italic;
    }
    </style>
    </head>
    <body>
      <h2>Troop 97 Permission Slip</h2>
    
      
      <form method="post" action="permissiontest.php">
        <label for="activity">Activity:</label>
        <input type="text" id="activity" name="activity" /><br />
        <label for="when">Date(s):</label>
        <input type="text" id="when" name="when" /><br />
        <label for="location">Location</label>
        <input type="text" id="location" name="location" /><br />
        <label for="scoutname">Scout Name(s)</label>
        <input type="text" id="scoutname" name="scoutname" /><br />
        
        <input type="submit" value="Submit Form" name="submit" />
      </form>
    </body>
    </html>
    HTML:
    <?php
    $activity = $_POST['activity'];
    $when = $_POST['when'];
    $location = $_POST['location'];
    $scout_name = $_POST['scoutname'];
    
    
    $dbc = mysql_connect(localhost, '*******', '******');
    mysql_select_db('troop97_permissions', $dbc)
    or die('Error connecting to MYSQL server.');
    
    $query = "INSERT INTO permission_test (activity, when, location, scout_name, "
    "VALUES ('$activity', '$when', '$location', '$scout_name', " ;
    
    $result = mysql_query($query, $dbc)
    or die(mysql_error().'<br>SQL:'.$query); 
    
    mysql_close($dbc);
    
    echo 'Thanks for submitting the permission form.<br>';
    
    ?>
    PHP:
    Here is the error message:
    Parse error: syntax error, unexpected '"' in /home/troop97/public_html/permissiontest.php on line 24
     
    dmwesq, Dec 8, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Change the query to

    
    $query = "INSERT INTO permission_test (activity, when, location, scout_name) VALUES ('$activity', '$when', '$location', '$scout_name');";
    
    PHP:
    If it will not help, write here the line 24 of the file permissiontest.php.
     
    s_ruben, Dec 9, 2010 IP
  3. hardik_dan

    hardik_dan Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    in your variable $query bracket for values is missing.
     
    hardik_dan, Dec 9, 2010 IP
  4. Zetiz

    Zetiz Active Member

    Messages:
    668
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #4
    I agree..The values you are entering into the table permission_test should be within single quotes individually. Then the line must terminate with a semi colon and the total query should be within double quotes.
     
    Zetiz, Dec 9, 2010 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    If you wanted ? a stripped down version, this would be the bare minimum.
    
    <?php
    $activity = $_POST['activity'];
    $when = $_POST['when'];
    $location = $_POST['location'];
    $scout_name = $_POST['scoutname'];
    
    mysql_connect(localhost, '*******', '******');
    mysql_select_db('troop97_permissions');
    
    $query = "INSERT INTO permission_test (activity, when, location, scout_name)VALUES('$activity', '$when', '$location', '$scout_name')";
    $result = mysql_query($query);
    
    echo "Thanks for submitting the permission form.<br>";
    
    ?>
    
    PHP:
     
    MyVodaFone, Dec 9, 2010 IP
  6. dmwesq

    dmwesq Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks - this last version of code worked. I haven't had time to look it over and compare to what was in the book I was using as a reference guide, but will try to review it carefully today and see where the differences were. Much of what I was using was code that was in the exercises in the book adapted to this new database. They worked as constructed in the book when I tested them, so now I need to learn from this experience and build up from there. Thanks to all for the input.
     
    dmwesq, Dec 9, 2010 IP
  7. dmwesq

    dmwesq Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I think I spoke too soon. I thought it worked as it echoed that the submission was sent. Upon checking database nothing was received. I put back the or die portion and got back the error querying database message. I'm wondering if it has anything to do with the difference between scoutname and scout_name?
     
    dmwesq, Dec 9, 2010 IP
  8. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #8
    Sorry my bad, try this instead, but please do use your book, what I posted is just a stripped down version, I simple eliminated the error reporting and a few unnecessary variables to achieve what you wanted.

    
    $activity = $_POST['activity'];
    $when = $_POST['when'];
    $location = $_POST['location'];
    $scout_name = $_POST['scoutname'];
    
    mysql_connect(localhost, '*******', '******');
    mysql_select_db('troop97_permissions');
    
    $query = "INSERT INTO permission_test (activity, when, location, scout_name)VALUES('$activity', '$when', '$location', '$scout_name')";
    mysql_query($query);
    
    echo "Thanks for submitting the permission form.<br>";
    PHP:
    You could also do it this way:
    
    
    mysql_connect(localhost, '*******', '******');
    mysql_select_db('troop97_permissions');
    
    mysql_query("INSERT INTO permission_test (activity, when, location, scout_name)VALUES(' . $_POST['activity'] . ', ' . $_POST['when'] . ', ' . $_POST['location'] . ', ' . $_POST['scoutname'] . ')");
    
    echo "Thanks for submitting the permission form.<br>";
    
    PHP:
     
    Last edited: Dec 9, 2010
    MyVodaFone, Dec 9, 2010 IP
  9. dmwesq

    dmwesq Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    * Edit post
    * Delete post
    * Report this post
    * Reply with quote

    Re: Code Help Please

    Postby dmwesq » Thu Dec 09, 2010 11:46 am
    Here is my latest error message:

    A fatal MySQL error occured.
    Query: INSERT INTO permission_test (activity, when, location, scout_name)VALUES('Harriman Backpacking', 'Oct', 'Harriman Park', 'Ima Eagle')
    Error: (1064) 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 'when, location, scout_name)VALUES('Harriman Backpacking', 'Oct', 'Harriman Park'' at line 1

    Server version: 5.0.91-community
     
    dmwesq, Dec 9, 2010 IP
  10. dmwesq

    dmwesq Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I am also wondering if the problem lies with the collation settings for my database. My default code is set to utf8_unicode but even though I actually set this as well when setting up my database it keeps reverting to latin1_swedish_ci for the database. I'm sure this has got to be part of the problem, but I have no idea why it keeps changing.
     
    dmwesq, Dec 9, 2010 IP
  11. dmwesq

    dmwesq Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Thanks for the responses - between the combination of cleaning up the code and avoiding protected words I now have it functioning and can move on to further developing the form.
     
    dmwesq, Dec 9, 2010 IP