Error Connecting To Mysql

Discussion in 'Databases' started by trenttdogg, Feb 10, 2013.

  1. #1
    I keep getting this error when trying to connect to 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 'desc, img, pdf, ccore, ccore_var) VALUES ('writing','friendly letters','hearts f' at line 1
    Code (markup):
    Does anyone know what I am doing wrong?
     
    trenttdogg, Feb 10, 2013 IP
  2. Colleen

    Colleen Illustrious Member

    Messages:
    6,777
    Likes Received:
    725
    Best Answers:
    1
    Trophy Points:
    430
    #2
    Can you post the code from the script that is trying to connect?
     
    Colleen, Feb 11, 2013 IP
  3. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #3
    Yes. Thanks
    <?php
    $con = mysql_connect("localhost","sshirk_tf","tfdb1");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
     
    mysql_select_db("sshirk_teachfuzz", $con);
     
    $sql="INSERT INTO worksheets (id, subject, category, title, desc, img, pdf, ccore, ccore_var)
    VALUES
    ('$_POST[id]','$_POST[subject]','$_POST[category]','$_POST[title]','$_POST[desc],''$_POST[jpeg]','$_POST[pdf]','$_POST[ccore]','$_POST[ccore_var]')";
     
    if (!mysql_query($sql,$con))
      {
      die('Error: ' . mysql_error());
      }
    echo "1 record added";
     
    mysql_close($con);
    ?> 
    Code (markup):
    It returns this error:
    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 'desc, img, pdf, ccore, ccore_var) VALUES ('','reading','Comprehension','Book Rep' at line 1
     
    trenttdogg, Feb 11, 2013 IP
  4. Colleen

    Colleen Illustrious Member

    Messages:
    6,777
    Likes Received:
    725
    Best Answers:
    1
    Trophy Points:
    430
    #4
    Colleen, Feb 11, 2013 IP
  5. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #5
    Hi Colleen,
    I tried the msqli_connect and was successful at making a connection.
    
    <?php
    /* Connect to an ODBC database using driver invocation */
    $dsn = 'mysql:dbname=sshirk_teachfuzz;host=184.173.226.215';
    $user = 'sshirk_tf';
    $password = 'tfdb1';
     
    try {
        $dbh = new PDO($dsn, $user, $password);
    } catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
    }
     
    ?>
    Code (markup):
    Question now is. How do I combine that connection piece of code into the form from the above post?
    Thanks
     
    trenttdogg, Feb 11, 2013 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Look at the error message. desc is a reserved word. If you can't redo your table to use a different field name, quote the field name.
     
    Rukbat, Feb 12, 2013 IP
  7. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #7
    Thanks Rukbat,
    I actually figured that out and got it corrected. However I have a new issue. See this post:
    https://forums.digitalpoint.com/threads/php-form-to-db.2635656/
    Thanks again.
     
    trenttdogg, Feb 12, 2013 IP