Update Query
How Modify data into DataBase.

For Modify Data use the update query:

Syntax:-
UPDATE table_name
SET column1=value, column2=value2, column3=value3…
WHERE anycolumn=value

anycolumn means you write which column you want to modify and write its value in update query
Note:- where clause is necessary otherwise all records will be update.

Update Data through PHP & Mysql

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

$con = mysql_connect($server,$username,$password);
if (!$con)
die('Mysql not connect ');

$database=mysql_select_db("test", $con);
if (!$database)
die('DataBase not connect ');

$result=mysql_query("update info set username='shishirphpdeveloper',email='shishirphpdeveloper@gmail.com' where id=3");

if($result)
echo "Record Update";
else
echo "Record not Update";

mysql_close($con);
?>

Update Data through PHP & Mysqli (Procedural)

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

$conn = mysqli_connect($server, $username, $password, $database);

if(!$conn){
die("Not Connected");
}

$query="update info set username='sunilphpdeveloper',email='sunilphpdeveloper@gmail.com' where id=3";
$result=mysqli_query($conn,$query);

if($result)
echo "Record Update";
else
echo "Record not Update";

mysqli_close($conn)
?>

Update Data through PHP & Mysqli (Object oriented)

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

$conn = new mysqli($server, $username, $password, $database);

if($conn->connect_error){
die("Not Connected");
}

$query="update info set username='rahulphpdeveloper',email='rahulphpdeveloper@gmail.com' where id=3";
$result=$conn->query($query);

if($result)
echo "Record Update";
else
echo "Record not Update";

mysqli_close($conn)
?>

Update Data through PHP & Mysql (PDO)

<?php
$server = "localhost";
$username = "root";
$password = "";
$database = "test";

try {
$conn = new PDO("mysql:host=$server;dbname=$database", $username, $password);

$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$query="update info set username='deepakphpdeveloper',email='deepakphpdeveloper@gmail.com' where id=3";

$conn->exec($query);
echo "Record updated successfully";
}
catch(PDOException $e)
{
echo "MySQL Not Connected";
}

$conn = null;
?>


Previous
Next