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     673
Rating:   4.50   out of 5 from   2   raters      
         rate this page   
 Not rated yet.Be the first one to vote!!

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



Updating a table


 We learned how to insert and retrieve data, let's have a look how to update it,now:

We have a table with a row like this one:
Advanced Database Technology and Design, Mario Piattini and Oscar Diaz, Database, 89

If we have to change the price of$ 89 to 95.20 $ for example,all we do is just a piece of code like the following

    <?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");


    //echo $rows_affected;  

    $sql="UPDATE books SET price = '$ 95,2' WHERE id=11" ;


    $result = mysql_query($sql);
    if (!$result) {
    die('Invalid query: ' . mysql_error());
    }
    $rows_affected =mysql_affected_rows();
    echo $rows_affected;
    ?>

    The above code, if succeds, return 1 as the row effected by the code is 1,or else return false and it will stop and print an error message.

    The only difference from inserting is this part
    $sql="UPDATE books SET price = '$ 95,2' WHERE id=11" ;


    $result = mysql_query($sql);
    if (!$result) {
    die('Invalid query: ' . mysql_error());
    }
    $rows_affected =mysql_affected_rows();
    echo $rows_affected;
    The $sql variable holds the sql statement and the PHP function mysql_query($sql); executes the command and the job is done