Update Data In a MySQL Table Using PHP functions and MySQL

Update Data In a MySQL Table Using MySQL and PHP functions

The UPDATE query statement :

UPDATE Database_Name.Table_name SET col1=value, col2=value2  WHERE column=value



using mysql is mysql query :

mysql_query("UPDATE  `erp`.`employee` SET  `address` =  'london' WHERE  `employee`.`id` =3");


Example STEP 1 create data.php

<?php

function DB_Mysql() {

     $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = 'root';
      $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      if(! $conn ) {
         die('Could not connect: ' . mysql_error());
      }
return $conn;
}

add this function to the data.php

function updateData($conn,$tableName,$db,$name,$email,$address,$slug,$id) {


$sql = "UPDATE  `erp`. $tableName SET  `name` =  '".$name."', `address` =  '".$address."',`email` ='".$email."',`slug` ='".$slug."'  WHERE  `employee`.`id` =".$id.";";
          mysql_select_db(''.$db.'');
$retval = mysql_query( $sql, $conn );

return $retval;
}

call function and pass variable to update data

$conn=DB_Mysql();

updateData($conn,$tableName,$db,$name,$email,$address,$slug,$id);

?>

Create a table in mysql

CREATE TABLE IF NOT EXISTS `employee` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(128) NOT NULL,
  `email` text NOT NULL,
  `address` varchar(128) NOT NULL,
  `slug` varchar(128) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `slug` (`address`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;


Final Result data.php file

<?php

function DB_Mysql() {

     $dbhost = 'localhost';
      $dbuser = 'root';
      $dbpass = 'root';
      $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      if(! $conn ) {
         die('Could not connect: ' . mysql_error());
      }
return $conn;
}

function updateData($conn,$tableName,$db,$name,$email,$address,$slug,$id) {


$sql = "UPDATE  `erp`. $tableName SET  `name` =  '".$name."', `address` =  '".$address."',`email` ='".$email."',`slug` ='".$slug."'  WHERE  `employee`.`id` =".$id.";";
          mysql_select_db(''.$db.'');
$retval = mysql_query( $sql, $conn );

return $retval;
}

$conn=DB_Mysql();
$tableName="employee";
$db="emp";
 $name="jack";
 $email="jack@gmail.com";
 $address="london";
 $slug="artist";

updateData($conn,$tableName,$db,$name,$email,$address,$slug,$id);

?>




No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...