How to Update Data into Database Using PHP/MySQLi

Today, you will learn How to Update Data into Database Using PHP/MySQLi. This method will show you on how to Update Data into Database with the use of update query statement in PHP and MySQLi.

Below are following step by step guide on how to Update Data Into Database Using PHP and MySQLi.

Lets Begin:

First Step: Create a MySQL Database and name it “userdb“.

Second: Do the following code for creating a table in the database that you have created.

[mysql]

CREATE TABLE IF NOT EXISTS `tbluser` (
`UserID` int(11) NOT NULL AUTO_INCREMENT,
`Name` varchar(30) NOT NULL,
`Username` varchar(30) NOT NULL,
`Pass` varchar(90) NOT NULL,
PRIMARY KEY (`UserID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

[/mysql]

Third Step:Insert the data using the following code below.

[mysql]

INSERT INTO `tbluser` (`UserID`, `Name`, `Username`, `Pass`) VALUES
(1, 'Janno Palacios', 'janobe', 'admin'),
(2, 'Joken villanueva', 'joken', 'joken'),
(3, 'kejie palacios', 'kenjie', 'kenjie');

[/mysql]

Fourth Step: Create a CSS file for the layout of the page and name it “style.css“.

[css]

#box{
width: 100%;
}
.column {
width: 33.33%;
display: inline-block;
}
.form{
padding: 2px;
width: 100%;
display: inline-block;
}

.first-column {
display: inline-block;
width: 100px;
height: 2px;
margin: 2px;
position: inherit;
}
.second-column{
display: inline-block;
width: 150px;
height: 2px;
margin: 2px;
position: inherit;
}
.btn{
width: 50px;
height: 40px;
margin: 2px;
float: left;
}
.clear{
clear: left;
height: 15px;
}

.table{
width: 100%;
border: solid 1px #ddd;
}
.table tr,
.table td {
position: inherit;
border: solid 1px #ddd;
}

[/css]

Fifth Step: Create a landing page and name it “index.php“.

Sixth Step: Do the following code for retrieving data in the database.

[php]

<html>
<title>Update Data into Database Using PHP</title>
<head>

<a href="http://style.css">http://style.css</a>
&lt;h1 align="center"&gt;List of Users&lt;/h1&gt;
&lt;/head&gt;
&lt;?php
$server="localhost";
$userid ="root";
$Password = "";
$myDB = "userdb";

$con = mysqli_connect($server,$userid,$Password,$myDB);

if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?&gt;
&lt;body&gt;
<div id="box">
<div class="column"></div>
<div class="column">

$sqli="SELECT * FROM tbluser";
$result=mysqli_query($con,$sqli) or die("query error");
while($row=mysqli_fetch_array($result)) {
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
echo '

';
}
}
?&gt;
<table class="table">
<tbody>
<tr>
<td>User ID</td>
<td>Name</td>
<td>Username</td>
<td>Action</td>
</tr>
<tr>
<td>'.$row['UserID'].'</td>
<td>'.$row['Name'].'</td>
<td>'.$row['Username'].'</td>
<td><a href="edit.php?id='.$row['UserID'].'">Edit</a></td>
</tr>
</tbody>
</table>
</div>
<div class="column"></div>
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
&lt;?php mysqli_close($con); ?&gt;[/php]

Seventh Step: Do the following code for filling the corresponding data into the text field.

[php]

&lt;?php
$server="localhost";
$userid ="root";
$Password = "";
$myDB = "userdb";

$con = mysqli_connect($server,$userid,$Password,$myDB);

if (mysqli_connect_errno()) {
# code...
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

if (isset($_GET['id'])) {
# code...
$sqli = "SELECT * FROM tbluser WHERE UserID=".$_GET['id'];
$result = mysqli_query($con, $sqli);
$details = mysqli_fetch_assoc($result);
}else{
header('Location: index.php');
}

?&gt;
<a href="http://style.css">http://style.css</a>
&lt;h1 align="center"&gt;Update Users&lt;/h1&gt;
<div id="box">
<div class="column"></div>
<div class="column">
<form class="form" action="edit.php " method="POST">
<label class="first-column ">User ID:</label>
" &gt;
<div class="clear"></div>
&lt;label class="first-column "&gt;Name:&lt;/label&gt;
&lt;input class="second-column" type="text" name="name" value="&lt;?php echo $details['Name']; ?&gt;" &gt;
<div class="clear"></div>
&lt;label class="first-column"&gt;Username:&lt;/label&gt;
&lt;input class="second-column" type="text" name="username" value="&lt;?php echo $details['Username']; ?&gt;" &gt;
<div class="clear"></div>
&lt;label class="first-column "&gt;Password:&lt;/label&gt;
&lt;input class="second-column" type="password" name="pass" value="&lt;?php echo $details['Pass']; ?&gt;" &gt;
<div class="clear"></div>
<div class="first-column "></div>
&lt;button class="btn second-column" name="save" type="submit"&gt;Save&lt;/button&gt;
&lt;/form&gt;

&lt;h1 align="center"&gt;List of Users&lt;/h1&gt;
&lt;table class="table" &gt;
&lt;tr&gt;
&lt;td&gt;User ID&lt;/td&gt;
&lt;td&gt;Name&lt;/td&gt;
&lt;td&gt;Username&lt;/td&gt;
&lt;td&gt;Action&lt;/td&gt;
&lt;/tr&gt;
&lt;?php list(); ?&gt;
&lt;/table&gt;
&lt;/div&gt;
<div class="column"></div>
&lt;/div&gt;
&lt;?php
function list(){
$sqli = "SELECT * FROM `tbluser`";
$result = mysqli_query($con,$sqli) or die("query error");

if ($result) {
# code...
while ($row = mysqli_fetch_array($result)) {
# code...
echo '&lt;tr&gt;';
echo '&lt;td&gt;'.$row['UserID'].'&lt;/td&gt;';
echo '&lt;td&gt;'.$row['Name'].'&lt;/td&gt;';
echo '&lt;td&gt;'.$row['Username'].'&lt;/td&gt;';
echo '&lt;td&gt;&lt;a href="edit.php?id='.$row['UserID'].'"&gt;Edit&lt;/a&gt;
&lt;/td&gt;';
echo '&lt;/tr&gt;';
}
}
}
mysqli_close($con);
?&gt;

[/php]

Eight Step: Do the following code for updating data in the database.

[php]

&lt;?php
if (isset($_POST['save'])) {
# code...
$id = $_POST['UserID'];
$name = $_POST['name'];
$username = $_POST['username'];
$pass = $_POST['pass'];
$sqli = "UPDATE `tbluser` SET `Name`='{$name}', `Username`='{$username}', `Pass`='{$pass}' WHERE `UserID`='{$id}'";
$res = mysqli_query($con,$sqli);

if ($res) {
# code...
echo "Data has been updated in the database.";
header('Location: index.php');
}
}
?&gt;

[/php]

If you have any questions or suggestion about how to Update Data Into Database Using PHP and MySQLi, please feel free to contact us at our contact page.You can subscribe this site to see more of my tutorials.

Frequently Asked Questions

How does this PHP project work?

Built with vanilla PHP (no framework) and MySQL backend. Standard structure: form HTML, PHP script handlers, MySQL via PDO or mysqli, sessions for auth, Bootstrap for responsive layout. Ready to extend for BSIT capstone scope.

What PHP and MySQL versions does this project require?

Most projects in this batch run on PHP 7.4 to PHP 8.2 with MySQL 5.7+ or MariaDB 10+. A few older projects need PHP 5.6 (deprecated, use XAMPP 7.x). To run: install XAMPP / WAMP / Laragon, extract project to htdocs, import the included .sql file via phpMyAdmin, edit the connection file (usually config.php or db_connect.php) with your DB credentials, browse to the project URL in your browser.

How do I set up the database for this PHP project?

Open phpMyAdmin (http://localhost/phpmyadmin/ on XAMPP), create a new empty database with the name specified in the project’s config.php. Click the Import tab, choose the included .sql file, click Go. Then edit config.php (or includes/connection.php) with: ‘localhost’, your MySQL username (usually ‘root’), your MySQL password (usually blank for XAMPP), and the database name.

Can I use this PHP project for a BSIT capstone or thesis?

Yes, but extend it. A bare CRUD app is too narrow for full capstone scope. Add: user roles via session checks, reports/dashboards (Chart.js + AJAX), PDF exports (TCPDF library), email notifications (PHPMailer), real domain extension (analytics, audit log, multi-branch support). Pair with Chapter 1-5 documentation matching your panel’s rubric.

Why am I getting ‘connection error’ or ‘undefined function mysqli_connect’?

Three common PHP issues: (1) Connection error: Apache + MySQL services not running in XAMPP control panel, OR database name in config.php does not match what you created in phpMyAdmin. (2) ‘undefined function mysqli_connect’: MySQL extension not enabled, in php.ini uncomment extension=mysqli (then restart Apache). (3) ‘No such file or directory’: MySQL socket path wrong, use 127.0.0.1 instead of localhost in the connection string.

Where can I find more PHP projects with source code?

Browse the PHP Projects hub for the full library (300+ vanilla PHP systems). For modern PHP MVC alternatives see Laravel Projects (74 systems) or CodeIgniter Projects (32 systems). For BSIT-focused capstone idea lists see 150 Best Capstone Project Ideas.

Leave a Comment