PHP MySQL Inserting Values in Table

Posted by tutor | Posted in PHP Tutorial | Posted on 18-12-2009-05-2008

0

After creating a table you will need to insert records. You can use the INSERT INTO statement to add records to a database table.

There are two different forms to write this statement. The first form doesn’t specify the column names where the data will be inserted, only their values:

    INSERT INTO table_name VALUES (value1, value2, value3,…)

The second form specifies both the column names and the values to be inserted:

    INSERT INTO table_name (column1, column2, column3,…)
    VALUES (value1, value2, value3,…)
    For eg:

    <?php
    $conn = mysql_connect(”localhost”,”tutor”,”tutor123″);
    if (!$conn)
    {
    die(’Could not connect: ‘ . mysql_error());
    }
    mysql_select_db(”my_db”, $conn);
    mysql_query(”INSERT INTO Persons (Country, Capital)
    VALUES (’India’, ‘New Delhi’)”);
    mysql_query(”INSERT INTO Persons (Country, Capital)
    VALUES (’United States of America’, ‘Washington D.C.’)”);
    mysql_query(”INSERT INTO Persons (Country, Capital)
    VALUES (’Zimbabwe’, ‘Harare’)”);
    mysql_query(”INSERT INTO Country (Country, Capital)
    VALUES (’Poland’, ‘Warsaw’)”);
    mysql_close($conn);
    ?>

Write a comment

Twitter Users
Enter your personal information in the form or sign in with your Twitter account by clicking the button below.