LinkShare  Referral  Program

New

Advertise ad Here
learn more
            PHP > Beginner PHP-MySql tutorial
Useful

Links


Free technical Tutorials At techtutorials.net



Link Exchange at iWEBTOOL.com

 



The PHP Resource Index
Added on   15.11.09
Clicks     981
Rating:   4.50   out of 5 from   2   raters      
         rate this page   
 Rate this page,Please

iBuyOfficesupply.com Inc. There is not Comments for this Article,Be the first one
Ask any relevant question in the Comments or leave your valuable feed-back

printer  Print version of this article

star  Add it to your favourite!

mail email this page to a frend
LinkShare  Referral  Program

Beginners PHP-MySql Tutorial



 Inserting Data




    In the previous 2 parts, we have learned   how to create a database and a table and now we are about to  insert some data in our table.

    We have a table called books, therefore ,let us put some books titles in it,as you can see on  the following  part 

    ID Title Author Category Price
    1 Advanced Database Technology and Design   Mario Piattini and Oscar Diaz  Database  $ 89.00  
    2 Conceptual Database Design  Carol Batini, Shamkant Navathe  Database  $ 96.80 
    3 A Guide to MySQL  Philip J. Pratt  MySql  $ 55.35 
    4 Beginning Databases with MySQL  Richard Stones  MySql  $ 48.90 


    <?php


    $host = "localhost"; // your host name
    $username = "root"; // your user name to access MySql
    $password = "prccn5021"; // your password to access MySql
    $database = "db_guestbook"; // The name of the database

    //$link = mysql_connect($host,$username,$password);
    if (!$link  = @mysql_connect($host,$username,$password,true))
    {die('Could not connect:'. mysql_error()); }

    @mysql_select_db($database) or die( "Unable to select database");

    $sql="INSERT INTO books ( title, author, category, price) VALUES ('Advanced Database Technology and Design', 'Mario Piattini and Oscar Diaz', 'Database','$ 89.00' ),
    ('Conceptual Database Design', 'Carol Batini Shamkant Navathe', 'Database', '$ 96.80'), ('A Guide to MySQL' ,'Philip J.Pratt', 'MySql', '$ 55.35'),
    ('Beginning Databases with MySQL', 'Richard Stones', 'MySql', '$ 8.90')";

    if(!$result = @mysql_query($sql,$link )){
    echo('Could not insert:'.@mysql_error());
    return false;}


    $rows_affected = mysql_affected_rows();
    echo $rows_affected;
    ?>

     The above code ouputs 4 because we inserted 4 rows at once.We may insert 1 row at the time but if you need,like in this case to save time ,to insert multiple rows,just follow this example.We started ,as usual,with the variables declaration and the we establish a connection to MySql and select a Database to work with,after that we assign the $sql variable the sql statement $sql="INSERT INTO books ( title, author, category, price) VALUES ('Advanced Database Technology and Design', 'Mario Piattini and Oscar Diaz', 'Database','89.00' ),
    ('Conceptual Database Design', 'Carol Batini Shamkant Navathe', 'Database', '96.80'), ('A Guide to MySQL' ,'Philip J.Pratt', 'MySql', '55.35'),
    ('Beginning Databases with MySQL', 'Richard Stones', 'MySql', '48.90')";
    as you can see the statements that use VALUES syntax can insert multiple rows. To do this, include multiple lists of column values, each enclosed within parentheses and separated by commas
    INSERT
    INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
    but to INSERT only 1 row just use the following
    INSERT INTO tbl_name ((a,b,c) VALUES(1,2,3);

    That's it,we have just inserted our 4 rows into the database,in the next tutorial,we'll see how to retrieve these books and manipulate the data.