AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

PHP MySQLi Update Data


<< Previous Page
PHP tutorial
Next Page >>
PHP tutorial

PHP Tutorial » PHP MySQLi Update Data

If you need to update a table from your database, you can use the command UPDATE.

Syntax:

UPDATE table_name
SET column1=value1, column2=value2, column3=value3,...
WHERE some_column=some_value
Note: WHERE clause specifies which record should  be updated. If you omit WHERE clause, all records will be updated!

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 Email
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:

Example (this is HTML - PHP editor, change text on this window)

<?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)

Example (this is HTML - PHP editor, change text on this window)

<?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

Example (this is HTML - PHP editor, change text on this window)

<?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 Email
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

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Home
PHP Introduction
PHP Install
PHP Syntax
PHP Variables
PHP Echo and Print
PHP Data Types
PHP String Function
PHP Constants
PHP Operator Types
PHP If Else and Elseif
PHP Switch
PHP While Loops
PHP Loop For Do Foreach
PHP Array
Convert Array to String
PHP Function
PHP GET and POST
PHP Date/Time Functions
PHP Login
PHP Delete Element
PHP eregi_replace()
PHP mysql_query()
PHP Errors to Display
PHP Loop For Do Foreach

PHP forms
PHP Form Example
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


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


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.