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
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Adding to MySQL DB

    I have this code:
    PHP Code:
     $query="INSERT INTO email(email,name VALUES($email,$name)";
      
    $result=mysql_query($query); 
    and in my DB I have a table called email, with to columns, email and name, why isn't this script working?

  • #2
    New to the CF scene
    Join Date
    Jun 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    should be:

    PHP Code:
    $query="INSERT INTO email VALUES($email,$name)"
    mysql_query($query); 
    -j

  • #3
    Senior Coder gsnedders's Avatar
    Join Date
    Jan 2004
    Posts
    2,340
    Thanks
    1
    Thanked 7 Times in 7 Posts
    Still doesn't work, here's my full code:
    PHP Code:
    <?PHP
    require ('config.php');

     
    mysql_connect(localhost,$db_user,$db_pass);
     @
    mysql_select_db($db) or die( "Unable to select database");
     
    $query="INSERT INTO email VALUES($email,$name)";
     
    mysql_query($query);

     
    mysql_close();
     
    ?>
    Config just has some varibiles in it.

  • #4
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    as far as I can see, you will need to tell where to insert these values.
    PHP Code:
    $query=
    "INSERT INTO emailDB 
    SET email = '$email'
    , name = '$name'"

    you can also use the parantheses with VALUES but I like it better like that.

    Don't forget the single quotes as the values you are inserting are not only numbers.

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    INSERT INTO emailDB (email,name) VALUES ($email,$name)

    How are $email and $name set? Via a form or url? If so, you need to use the superglobals, ie;
    $email = $_GET['email'];
    $name = $_GET['name'];
    at the top of the page, use $_POST instead of $_GET if you're using the POST method in the form

  • #6
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    btw this thread makes wonder what are the differences between using INSERT ... SET
    and
    INSERT... () VALUES ().

    sorry for hijacking your thread error404

  • #7
    New Coder
    Join Date
    Jun 2004
    Location
    Newcastle, England
    Posts
    72
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeh i always use SET....afaik they are exactly the same
    Beauty is in the eye of the beer-holder

    Man who smoke pot, choke on handle

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    There's no difference. There's 3 ways to INSERT

    INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    VALUES ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

    Or:

    INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
    [INTO] tbl_name
    SET col_name={expr | DEFAULT}, ...
    [ ON DUPLICATE KEY UPDATE col_name=expr, ... ]

    Or:

    INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    SELECT ...

    It's all down to your preference really.

  • #9
    Regular Coder
    Join Date
    Aug 2003
    Posts
    565
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for your replies. you confirmed what I expected


  •  

    Posting Permissions

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