articles

Updating and Deleting Record in MySQL Database Table using PHP

Anonymous User9386 21-Sep-2011
Updating Record:

UPDATE statement is used for update existing records in MySQL database table.

Syntax:

UPDATE table_name SET column_name1 = value1, column_name2 = value2,..... WHERE column_name = column_value  

Note: WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated.

Example:
       <?php
        // create connection
        $con = mysql_connect("localhost", "root", "root");
        // check connection is created or not
        if(!$con)
        {
            die ("Conection not created. Please try again !");
        }
        // select database
        mysql_select_db('tempdatabase',$con);
 
        // update query
        $qry ="Update tblTemp set EmailId = 'username@hostname.com' where id = 102";
        // execute the queryand check updation
        if(mysql_query($qry))
        {
            echo 'Data updated successfully';
        }
        else
        {
            echo  'Updation failed' ;
        }
   ?>
Output:

Updating and Deleting Record in MySQL Database Table using PHP

Deleting Record:

DELETE FROM statement is used to delete records from database table.

Syntax:

DELETE FROM tbl_Name WHERE column_name= value

Example:

Look at the following ‘tblTemp’ table.

Updating and Deleting Record in MySQL Database Table using PHP

Now we want to delete record from ‘tblTemp’ table where id is103. Let’s have an example to performing this action.

<?php
        // create connection
        $con = mysql_connect("localhost", "root", "root");
        // check connection is created or not
        if(!$con)
        {
            die ("Conection not created. Please try again !");
        }
        // select database
        mysql_select_db('tempdatabase',$con);
 
        // Delete query
        $qry ="Delete from tblTemp where id =103";
        // execute the queryand check updation
        if(mysql_query($qry))
        {
            echo 'Record deleted successfully';
        }
        else
        {
            echo  'Record could not deleted.' ;
        }
   ?>

Note: WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted from given table.

Output:

Updating and Deleting Record in MySQL Database Table using PHP

Now look ‘tblTemp’ table records.

Updating and Deleting Record in MySQL Database Table using PHP



Updated 04-Mar-2020
I am a content writter !

Leave Comment

Comments

Liked By