codescracker


php

PHP and MySQLi Update Record



« Previous Tutorial Next Tutorial »


Here you will learn about how to update record or data into the table using PHP with MySQLi.

What to do Before Updating Record

Before updating the record into the table, be sure that you have already learned our previous tutorial and created a database and a table namely codescrackerTwo and codescrackerTable and inserted a record into that table.

Now, let's learned how to update the record or data that are present inside the table using PHP with MySQLi.

PHP and MySQLi Update Record Example

Here is an example demonstrates about updating the record into the MySQL table using PHP with MySQLi.

Important - Be sure that you have given the id number or anything unique (that is primary key such as username, id number, product id, student id, etc.) before updating the record into the table. If you will not provide any primary key value when updating the record, then all the record will be updated.

<?php 
	$serverName = "localhost";
	$username = "root";
	$password = "";
	$databaseName = "codescrackerTwo";
	$conn = mysqli_connect($serverName, $username, $password, $databaseName);
	if(mysqli_connect_error())
	{
		echo "<p>Ooppss..error occurred in establishing the connection
				to the database...exiting...</p>";
		exit();
	}
?>
<html>
<head>
	<title>PHP and MySQLi Update Record Example</title>
</head>
<body>
<?php
	// here we are going to update the record that have id number 1
	// record with id number 1 is inserted in the previous tutorial
	// now let's update that record using the below code
	$idNumber = 1;
	$querySQL = "update codescrackerTable set usercomment='I just love to learn 
				everything here on codescracker.com' where id=$idNumber";
	$res = $conn->query($querySQL);
	if($res)
	{
		echo "<p>Record update successfully.</p>";
	}
	else 
	{
		echo "<p>Oooppsss..some error occurred...exiting..</p>";
		exit();
	}
?>
</body>
</html>

Here is the sample output produced by the above example code of updating the record using PHP and MySQLi.

php mysqli update record

And here is the screenshot of the table after updating the record with id number 1.

php mysqli update data

As you can see from the above example, we have only update the record of id number 1.

PHP MySQLi Update all Records

Now, here is an example demonstrates how to update all the record in the MySQL table using PHP and MySQLi.

As you have already learned in the above tutorial that how important is, providing the primary key value before updating the record into the table.

Here we are not going to provide any primary key value or id number (here) to update the record only for that id number.

If you will not provide any primary key value with WHERE clause or condition, then all the record will be updated as shown in the example given below.

<?php 
	$server = "localhost";
	$user = "root";
	$pass = "";
	$db = "codescrackerTwo";
	$conn = mysqli_connect($server, $user, $pass, $db);
	if(mysqli_connect_error())
	{
		echo "<p>Ooppss..error occurred in establishing the database connection.</p>";
		echo "<p>Exiting...</p>";
		exit();
	}
?>
<html>
<head>
	<title>PHP and MySQLi Update All Records Example</title>
</head>
<body>
<?php
	$querySQL = "update codescrackerTable set usercomment='I just love to learn 
				everything here on codescracker.com'";
	$res = $conn->query($querySQL);
	if($res)
	{
		echo "<p>All the records are update successfully.</p>";
	}
	else 
	{
		echo "<p>Oooppsss..some error occurred...exiting..</p>";
		exit();
	}
?>
</body>
</html>

Here is the sample output that will come out after running the above example code to update all the records using PHP.

update all record php mysqli

And below is the screenshot of the table on which, the above program (query) is executed.

php mysqli update record example

As you can see from the above figure that all the records are updated.


« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test