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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Error querying database :( HELP!

    Hey guys, I was just wondering if anyone could point out the mistake I'm making. Everything seems pretty okay to me but I ALWAYS get an error when the script try's to query the database. Sad. Any Ideas?
    PHP Code:
    <html>
    <head><title>Add Email</title></head>
    <body>
    <?php
    //START---->GET THE DATA
    $first $_POST['firstname'];
    $last $_POST['lastname'];
    $email $_POST['emailaddress'];
    //END

    //CONNECT TO DATA BASE
    $dbc mysqli_connect('address','user','pass','database');
    if(!
    $dbc){
        die(
    "Connection unsucessful...");
    }
    echo 
    "Connection successful..";
    //CREATE QUERY

    $q "INSERT INTO email_list('first_name','last_name','email')" "VALUES('$first', '$last', '$email')";
    //SEND QUERY
    $result mysqli_query($dbc$q
              or die(
    'Oops! Error querying database.');
    //CLOSE
    mysqli_close($dbc);

    //ECHO SUCCESSFUL
    echo "Data has been sent!";

    ?>
    </body>
    </html>
    NOTE: I always get Oops! Error querying database.

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,395
    Thanks
    13
    Thanked 353 Times in 349 Posts
    I'd say, look at the query you use (echo it out), there are spaces missing. mysqli_error() can certainly tell you more.

    PS. congratulation for not using the old mysql_* functions
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Change this line:
    PHP Code:
    or die('Oops! Error querying database.'); 
    ...to this:
    PHP Code:
    or die(
        
    '<pre>MySQL Error: ' mysql_error$dbc ) . "\n\n" .
        
    'MySQL Query: ' $q '</pre>'
    ); 
    ...and paste the message here.

  • #4
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    Change this line:
    PHP Code:
    or die('Oops! Error querying database.'); 
    ...to this:
    PHP Code:
    or die(
        
    '<pre>MySQL Error: ' mysql_error$dbc ) . "\n\n" .
        
    'MySQL Query: ' $q '</pre>'
    ); 
    ...and paste the message here.
    I get this:
    Code:
    Connection successful..
    
    {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 ''first_name','last_name','email') VALUES ('Random_name1', 'Random_name2', 'Rando' at line 1
    
    MySQL Query: INSERT INTO email_list('first_name','last_name','email') VALUES ('Random_name1', 'Random_name2', 'Random_email@flymail.com' )}
    So the syntax I'm using doesn't correspond with the version of the MYSQL server I'm using? I got that right?
    P.S:
    My MySQL server version is 5.1.36-community-log btw.

  • #5
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Remove the quotes

    Change
    Code:
    ('first_name','last_name','email')
    To
    Code:
    (first_name,last_name,email)
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • Users who have thanked DJCMBear for this post:

    DivineComedian (12-25-2010)

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DJCMBear View Post
    Remove the quotes

    Change
    Code:
    ('first_name','last_name','email')
    To
    Code:
    (first_name,last_name,email)
    Nope, that didn't work either

  • #7
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you everyone!!! After examining the code line by line, I found the problem! I should have put single quotes around the variables:firstname,lastname and email

    EDIT:
    And not around the table names.
    Last edited by DivineComedian; 12-25-2010 at 05:30 AM.

  • #8
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by DivineComedian View Post
    Thank you everyone!!! After examining the code line by line, I found the problem! I shouldn't have put single quotes around the variables:firstname,lastname and email
    That's what I said your problem was and you said it wasn't
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #9
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DJCMBear View Post
    That's what I said your problem was and you said it wasn't
    LOL! Oh right, poor me...I believe I must have dropped the single quotes around the variables aswell. In away I'm glad it happened 'cause I learnt a lot! Thanks again!!


  •  

    Posting Permissions

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