PHP MySQLi Update Data
PHP Tutorial » PHP MySQLi Update Data
If you need to update a table from your database, you can use the command UPDATE.
Syntax:
SET column1=value1, column2=value2, column3=value3,...
WHERE some_column=some_value
Let's look at the "Users" table: We will have six columns as: "id", "first_name", "last_name", "user_name", "email" and "reg_date":
How do you update data from this table in CSS?
ID | First Name | Last Name | UserName | |
---|---|---|---|---|
1 | Clark | Kent | clarkkent | clarkkent@mail.com |
2 | John | Carter | johncarter | johncarter@mail.com |
3 | Peter | Parker | peterparker | peterparker@mail.com |
In the next examples we UPDATE the record with id=2 in the "Users" table:
UPDATE data in MySQLi Table (object oriented)
In the next examples we Updata row id=2 in the "Users" table:
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = new mysqli($server_name, $user_name,
$password, $db_name);
// Check connection
if ($link->connect_error) {
die("Connection failed: " . $link->connect_error);
}// sql to delete a record
$sql = "UPDATE Users SET last_name='Kent' WHERE id=2";
if ($link->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updated record: " . $link->error;
}
$link->close();
?>
Related subjects:
How to create database phpmyadmin?
Insert multiple rows
PHP select / view data
Conect to MySQLi
PHP MySQLi create table
PHP MySQLi delete data
Tags: how to update data from database in php using button, how to update data in php using form, update mysql using html form and php, php mysql update multiple fields, how to update data in php using form pdo, how to update data in php using form mysqli, mysql update example, updating single record in mysql table with php using toggle button
Update data in DataBase MySQLi (Procedural)
In the next examples we Update the row with id=2 in the "Users" table using MySQLi (Procedural)
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
// Create connection
$link = mysqli_connect($server_name, $user_name,
$password, $db_name);
// Check connection
if (!$link) {
die("Connection failed: " . mysqli_connect_error());
}
// sql to delete a record
$sql = "UPDATE Users SET last_name='Kent' WHERE id=2";
if (mysqli_query($link, $sql)) {
echo "Record updated successfully";
} else {
echo "Error updated record: " . mysqli_error($link);
}
mysqli_close($link);
?>
Update row in DataBase MySQLi using (PDO) extension
The following example update a row in table named "Users" in database my_DB using PDO
<?php
$server_name = "localhost";
$user_name = "my_username";
$password = "my_password";
$db_name = "my_DB";
try {
$link = new PDO("mysql:host=$server_name;dbname=$db_name",
$user_name, $password);// set the PDO error mode to exception
$link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// sql to delete a record
$sql = "UPDATE Users SET last_name='Kent' WHERE id=2";
// Prepare statement
$state = $link->prepare($sql);// execute the query
$state->execute();// echo a message to say the UPDATE succeeded
echo $state->rowCount() . " records UPDATED successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}$link = null;
?>
ID | First Name | Last Name | UserName | |
---|---|---|---|---|
1 | Clark | Kent | clarkkent | clarkkent@mail.com |
2 | John | Kent | johncarter | johncarter@mail.com |
3 | Peter | Parker | peterparker | peterparker@mail.com |
how to update data from database in php using button, how to update data in php using form, update mysql using html form and php, php mysql update multiple fields, how to update data in php using form pdo, how to update data in php using form mysqli, mysql update example, updating single record in mysql table with php using toggle button
PHP MySQLi Update Data - php tutorial
This tool makes it easy to create, adjust, and experiment with custom colors for the web.

Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.

Find here examples of creative and unique website layouts.

Find here examples of creative and unique website CSS HTML menu.