SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    INSERTing values into mysql Table?

    Hi,

    Whats the code for inserting variables into a mysql table?

    Thank You!
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two ways:

    1)

    INSERT INTO tableName SET
    field1 = 'somevalue',
    field3 = 'somevalue3',
    field7 = 'somevalue7'

    This is handy if you only want to set some of the values and leave other fieldsNULL, or if they are set to be NOT NULL then they will be set to their DEFAULT values.

    2)

    INSERT INTO tableName VALUES
    ( 'somevalue', 'somevalue2', somevalue3'),
    ( 'somevalue', 'somevalue2', somevalue3')

    Here we do not specifiy the field names just supply the values. MySQL expects you to provide a value for each field in the table (even if that value is just NULL or an empty string ''.

    Note that with the second method you can insert multiple rows into the table. In the example above I have inserted two rows.

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    thanks freakysid.

    So an example of which would be:

    mysql_query("INSERT INTO dat_files VALUES ( '$name', '$userfile', '$description','$version','$category','$user')";

    ?
    its producing a parse error though.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Okay. I played around..and got this working.

    $data_query = "INSERT INTO dat_files VALUES ( '$name', '$userfile_name', '$description','$version','$category','$user')";
    $run_query = mysql_query($data_query, $db);

    what you think freakysid? thanks
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  5. #5
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You were getting a parse error because this
    Code:
    mysql_query("INSERT INTO dat_files VALUES ( '$name', '$userfile', '$description','$version','$category','$user')";
    is missing a parentheses. You opened it correctly after mysql_query but then you opened a second one for the values - so you would need this:
    Code:
    mysql_query("INSERT INTO dat_files VALUES ( '$name', '$userfile', '$description','$version','$category','$user')");
    HTH
    Kevin

  6. #6
    Victory shall be mine tubedogg's Avatar
    Join Date
    Mar 2001
    Location
    Medina, OH
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The second post you made looks just fine as well.
    Kevin


Bookmarks

Posting Permissions

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