Good Day!!!
Yesterday, we create a tutorials for
Inserting Data,
Select Data, and
Deleting Data from MySQL. For this follow-up tutorial, we are going to create another that related to them.
We are going to create
PHP Update Data In MySQL. For this tutorial, we are going to use the
UPDATE Statement to update the current data in our table.
This is the data in our table.
In the underline, that we are going to update the first name.
Updating Data Using MySQLi and PDO
Using MySQLi (Object-Oriented)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edit_query_pdo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE tbl_registration SET first_name='Sarah' WHERE tbl_registration_id=2";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
Using MySQLi (Procedural)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edit_query_pdo";
// Create connection
// Check connection
if (!$conn) {
}
$sql = "UPDATE tbl_registration SET first_name='Sarah' WHERE tbl_registration_id=2";
echo "Record updated successfully";
} else {
}
?>
Using PDO (PHP Data Objects)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "edit_query_pdo";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE tbl_registration SET first_name='Sarah' WHERE tbl_registration_id=2";
// Prepare statement
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
// echo a message to say the UPDATE succeeded
echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
And, this is another source code to update the record in the database table.
In this source code, you can edit one or more column in our table.
<?php
include 'connection.php';
$get_id=$_REQUEST['tbl_registration_id'];
$first_name= $_POST['first_name'];
$middle_name= $_POST['middle_name'];
$last_name= $_POST['last_name'];
$email= $_POST['email'];
$contact_number= $_POST['contact_number'];
$sql = "UPDATE tbl_registration SET first_name ='$first_name', middle_name ='$middle_name',
last_name ='$last_name', email ='$email', contact_number ='$contact_number' WHERE tbl_registration_id = '$get_id' ";
echo "<script>alert('Successfully Edit The Account!'); window.location='index.php'</script>";
?>
Output:
This is the Edit Form.
After the data updated, this is our new table.
Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at
[email protected]. Practice Coding. Thank you very much.