Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2012
    Posts
    2

    Unanswered: mysql syntax error

    Hi, I'm trying to input data from an html page with forms to a php script to my mysql database.

    Here's my code

    PHP Code:
    <?php
    $UserName 
    $_POST[UserName];
    $Password $_POST[Password];
    $Email $_POST[Email];

    mysql_connect("localhost""username""password") or die ('Error: ' mysql_error());
    mysql_select_db("a1298_userAccounts") or die("cannot find database");

    $query="INSERT INTO UserAccounts (Index, UserName, Password, Email)
    VALUES(NULL, '
    $UserName','$Password','$Email')";
            
    $result mysql_query($query);

    if(
    $result == false)
    {
        die (
    'Error: ' mysql_error());
    }
    ?>
    I'm new to php and mysql, but not programming I've been coding in c++ for a few years. Does my syntax seem to be ok? myphpadmin says my server version is 5.1.63-cll. My first column is named Index and I set it to auto-increment. My next one is UserName, then Password, then Email. I'm getting this error when the php code runs: 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 'Index, UserName, Password, Email) VALUES(NULL, 'shadyvillian','mypassword','addr' at line 1. I seem to be connecting to the database ok is this a problem with my database or the code?

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    the error message is telling you exactly where your sql statement is barfing

    INDEX is a reserved word

    rename that column
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    All db's reserve words that cannot be used for table or column names
    check the appropriate list for the version of MySQL you use, but for now MySQL :: MySQL 5.5 Reference Manual :: 9.3 Reserved Words
    I'd rather be riding on the Tiger 800 or the Norton

  4. #4
    Join Date
    Jun 2012
    Posts
    2
    Thank you, that fixed it. In my index column should I check the box for NULL? I know I'm passing NULL in the script because it auto increments but would that screw it up if I did that?

  5. #5
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    no, the auto_increment should be NOT NULL because it's the PRIMARY KEY
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

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