How to update data from database in php


Update Data In a MySQL Table Using MySQLi and PDO

The UPDATE statement is used to update existing records in a table:

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value 

Notice the WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

To learn more about SQL, please visit our SQL tutorial.

Let's look at the "MyGuests" table:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Moe 2014-10-23 10:22:30

The following examples update the record with id=2 in the "MyGuests" table:

Example (MySQLi Object-oriented)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . $conn->error;
}

$conn->close();
?>




Example (MySQLi Procedural)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if (mysqli_query($conn, $sql)) {
  echo "Record updated successfully";
} else {
  echo "Error updating record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>


Example (PDO)

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

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 MyGuests SET lastname='Doe' WHERE 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 . "
" . $e->getMessage();
}

$conn = null;
?>


After the record is updated, the table will look like this:

idfirstnamelastnameemailreg_date
1 John Doe 2014-10-22 14:26:15
2 Mary Doe 2014-10-23 10:22:30






To update a data that already exist in the database, UPDATE statement is used.

In the below example we update the employee data from MySQL database.

we used 2 file for update data

  • database.php- To connecting database.
  • update.php- TO retrieve data from database with a update option.
  • update-process.php- TO update data from database.

database.php


update.php



 
    Retrive data
   
 

 0) {
?>
Sl No First Name Last Name City Email id Action
">Update

update-process.php

0) {
mysqli_query($conn,"UPDATE employee set userid='" . $_POST['userid'] . "', first_name='" . $_POST['first_name'] . "', last_name='" . $_POST['last_name'] . "', city_name='" . $_POST['city_name'] . "' ,email='" . $_POST['email'] . "' WHERE userid='" . $_POST['userid'] . "'");
$message = "Record Modified Successfully";
}
$result = mysqli_query($conn,"SELECT * FROM employee WHERE userid='" . $_GET['userid'] . "'");
$row= mysqli_fetch_array($result);
?>


Update Employee Data


Username:

First Name:

Last Name :

City:

Email:


How to update data from database in php

How fetch and edit data from database in PHP?

How to Fetch and Update Data From MySQL Database in PHP.
Step 1 – Create PHP Project..
Step 2 – Execute SQL query to Create Table..
Step 3 – Connecting PHP App to Database..
Step 4 – Fetch Data From MySQL Database in PHP by Id..
Step 5 – Display Data In Html From MySQL DB with PHP..
Step 6 – Update Data From MySQL Database..

How do you update data in a database?

To update data in a table, you need to:.
First, specify the table name that you want to change data in the UPDATE clause..
Second, assign a new value for the column that you want to update. ... .
Third, specify which rows you want to update in the WHERE clause..

How do I update phpmyadmin data?

Use the navigation tree in the left sidebar to locate the database table you wish to modify..
The database fields are displayed in the right pane. ... .
You can now proceed to modify the data within the field..
If you wish to modify table details, select the Operations tab at the top of the screen..

How get fetch data from database in PHP?

Retrieve or Fetch Data From Database in PHP.
SELECT column_name(s) FROM table_name..
$query = mysql_query("select * from tablename", $connection);.
$connection = mysql_connect("localhost", "root", "");.
$db = mysql_select_db("company", $connection);.
$query = mysql_query("select * from employee", $connection);.