Results 1 to 8 of 8

Thread: insert into db - resolved

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260

    insert into db - resolved

    Hi Guys.
    im trying to insert a line into mysql with the follong code
    PHP Code:
    mysql_connect("localhost","root","InsertYourPasswordHere");
    mysql_select_db ("mywakeupcall");
    $query "INSERT INTO database(fname,lname,alarm,phone) VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['atime']."','".$_POST['telephone']."')"

    if (! 
    mysql_query($query))
        die (
    'Query Failed: ' mysql_error());

    echo 
    'Item Entered.'
    however the error msg that comes back is

    Query Failed: 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 'database(fname,lname,alarm,phone) VALUES('samantha','murray','1

    andI cant find out whatthe problem is. fname and lname is text and the alarm and phone is numbers.

    any help would be great thanks

    chrisio
    Last edited by Chrisio; Jun 9th, 2004 at 09:11 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    You have a table named "database"? I suggest changing it to something else. Database is most likely a reserved keyword, and shouldn't be used in tables/fields names.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260
    ok. table name changed and nodifference i get the same error message as before.

    PHP Code:

    <?php
    $temp1 
    $_POST['firstname'];
    $temp2 $_POST['lastname'];
    $temp3 $_POST['atime'];
    $temp4 $_POST['telephone'];

    mysql_connect("localhost","root","InsertYourPasswordHere");
    mysql_select_db ("mywakeupcall");

    $query "INSERT INTO tblmain ('fname', 'lname', 'alarm', 'phone' VALUES ($temp1$temp2$temp3$temp4)";

    if (! 
    mysql_query($query))
        die (
    'Query Failed: ' mysql_error());

    echo 
    'Item Entered.';

    ?>

    error message :

    Query Failed: 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 ''fname', 'lname', 'alarm', 'phone' VALUES (samantha, murray, 10


    im very puzzled.

    if i also just use

    INSERT INTO tblmain VALUES ($temp1, $temp2, $temp3, $temp4)";

    it throws back this error message

    Query Failed: Unknown column 'samantha' in 'field list'

    the field names in the db are as follows

    fname (varchar20)
    lname (varchar20)
    alarm (int4)
    phone (int11)

    thanks again

  4. #4
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    You can't just throw variables into a string without putting them in quotes. And when you are inserting, you have to enclose values in single quotes. Hence: singlequote|doublequote|dot|variablename|dot|doublequote|singlequote

    PHP Code:
    <?php
    mysql_connect
    ("localhost","root","InsertYourPasswordHere");
    mysql_select_db ("mywakeupcall");

    $query "INSERT INTO tblmain ('fname', 'lname', 'alarm', 'phone') VALUES ('" $_POST['firstname'] . "', '" $_POST['lastname'] . "', '" $_POST['atime'] . "', '" $_POST['telephone'] . "')";

    $pass = @mysql_query($query) or die('Query Failed: ' mysql_error());

    if (
    $pass)
    echo 
    'Item Entered.';
    ?>
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260
    Originally posted by ober0330
    You can't just throw variables into a string without putting them in quotes. And when you are inserting, you have to enclose values in single quotes. Hence: singlequote|doublequote|dot|variablename|dot|doublequote|singlequote

    PHP Code:
    <?php
    mysql_connect
    ("localhost","root","InsertYourPasswordHere");
    mysql_select_db ("mywakeupcall");

    $query "INSERT INTO tblmain ('fname', 'lname', 'alarm', 'phone') VALUES ('" $_POST['firstname'] . "', '" $_POST['lastname'] . "', '" $_POST['atime'] . "', '" $_POST['telephone'] . "')";

    $pass = @mysql_query($query) or die('Query Failed: ' mysql_error());

    if (
    $pass)
    echo 
    'Item Entered.';
    ?>
    hi.
    I have tried that and get the exate same error msg so imcompleaty stuck now.

    help?

  6. #6
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    Try this:
    PHP Code:
    <?php
    mysql_connect
    ("localhost","root","InsertYourPasswordHere");
    mysql_select_db ("mywakeupcall");

    $query "INSERT INTO tblmain (fname, lname, alarm, phone) VALUES ('" $_POST['firstname'] . "', '" $_POST['lastname'] . "', '" $_POST['atime'] . "', '" $_POST['telephone'] . "')";

    $pass = @mysql_query($query) or die('Query Failed: ' mysql_error());

    if (
    $pass)
    echo 
    'Item Entered.';
    ?>
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2002
    Location
    Newcastle
    Posts
    260
    PERFECT THANKS VERY MUCH

  8. #8
    Frenzied Member ober0330's Avatar
    Join Date
    Dec 2001
    Location
    OH, USA
    Posts
    1,945
    You're welcome.
    format your code!! - [vbcode] [/vbcode]

    ANSWERS CAN BE FOUND HERE!!

    my personal company

Posting Permissions

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



Click Here to Expand Forum to Full Width