How to insert/create a row in a table?

Discussion in 'MySQL' started by EGS, Apr 13, 2007.

  1. #1
    How can I go about creating/inserting a new row in a table in MySQL?
    What is the SQL query? Is it something as easy as creating a table (like CREATE ROW) or something? :confused:

    Please assist. :eek:
    Thanks.<33
     
    EGS, Apr 13, 2007 IP
  2. ottodo

    ottodo Guest

    Messages:
    2,055
    Likes Received:
    70
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    // Make a MySQL Connection
    mysql_connect("localhost", "admin", "1admin") or die(mysql_error());
    mysql_select_db("test") or die(mysql_error());
    
    // Insert a row of information into the table "example"
    mysql_query("INSERT INTO example 
    (name, age) VALUES('Timmy Mellowman', '23' ) ") 
    or die(mysql_error());  
    
    mysql_query("INSERT INTO example 
    (name, age) VALUES('Sandy Smith', '21' ) ") 
    or die(mysql_error());  
    
    mysql_query("INSERT INTO example 
    (name, age) VALUES('Bobby Wallace', '15' ) ") 
    or die(mysql_error());  
    
    echo "Data Inserted!";
    
    ?>
    Code (markup):
    source:
    I hope this helps :)
     
    ottodo, Apr 13, 2007 IP
  3. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #3
    That just looks like data is being inserted into a pre-existing row inside a table...
    Will it create the row if it doesn't exist?
     
    EGS, Apr 13, 2007 IP
  4. druidelder

    druidelder Peon

    Messages:
    285
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Insert is to input data into a new row, notice there is no where clause. If you were putting data into an existing row it would be an Update query, not an Insert.
     
    druidelder, Apr 13, 2007 IP
  5. ewriter

    ewriter Banned

    Messages:
    590
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    use insert for insertion, delete from for deletion and update for updating existing data.
     
    ewriter, Apr 14, 2007 IP
  6. EGS

    EGS Notable Member

    Messages:
    6,078
    Likes Received:
    438
    Best Answers:
    0
    Trophy Points:
    290
    #6
    Eh nevermind, appararently I meant I wanted to insert a field, not a row, and I was easily able to do it in phpMyAdmin. :D
     
    EGS, Apr 14, 2007 IP
  7. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #7
    Glad that it's done.
    Do some reading on "ALTER" table commands as well. :)
    Bye :)
     
    JEET, Apr 14, 2007 IP