Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow Wrong SQL Syntax

    My MySQL version is 5.0. I am trying to run this small query but getting this SQL error

    "Error! Could not insert valuesYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''userdata' ('fName', 'lName', 'email', 'confEmail', 'password', 'confirmPassword' at line 1"

    The syntax that I am working on is:

    $sql = "insert data 'userdata' ('fName', 'lName', 'email', 'confEmail', 'password', 'confirmPassword', 'address', 'city', 'state', 'postCode', 'gender', 'profession', 'ageGroup', 'mallPref', 'mailConsent') VALUES ($fName, $lName, $email, $confEmail, $password, $confirmPassword, $address, $city, $state, $postCode, $gender, $profession, $ageGroup, $mallPref, $mailConsent)";

    dont understand where is it wrong. Does capitalization of INSERT also matters. I am new to this. Please advice.

    Thanks in advance.

    Chris

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    It's always helpful to include in your error handling a display of the query string as it looks all put together. A simple "echo $sql;" statement can really help.

    As it looks to me, you need to enclose your table name in `backticks` (the tick just above your tab key and to the left of your 1 key) rather than normal single quotes. You can omit those ticks altogether if you name your tables properly (i.e. don't name them a MySQL reserved word or something equally silly).

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,169
    Thanks
    19
    Thanked 65 Times in 64 Posts
    Have you tried

    Code:
    $sql = "insert into userdata ('fName', 'lName', 'email', 'confEmail', 'password', 'confirmPassword', 'address', 'city', 'state', 'postCode', 'gender', 'profession', 'ageGroup', 'mallPref', 'mailConsent') VALUES ($fName, $lName, $email, $confEmail, $password, $confirmPassword, $address, $city, $state, $postCode, $gender, $profession, $ageGroup, $mallPref, $mailConsent)";
    Maybe you know something I dont but I've never seen 'INSERT DATA' in mysql before

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    As NancyJ says + you need to add single quotes around text fields ... so
    Code:
    '$fName', '$lName', '$email'
    etc

    personally I prefer the INSERT INTO `table` SET style since you can use the same SQL for INSERTS, UPDATES or REPLACE's

    PHP Code:
    <?php
    $sql
    ="`field1`='$field1', `field2`='$field2'";
    $sql "INSERT INTO `table` SET $sql";
    //or
    $sql="UPDATE `table` SET $sql";
    //etc 
    ?>
    note that the `'s around the field and table names are backticks not single quotes.
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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