php mysqli update - Web Development and Web Design Codes

Latest

Thursday, April 27, 2017

php mysqli update

Update Data Using PHP MySQLi

php mysqli update data from database
php mysqli update data from database

Hi Guys in This Tutorial we will Learn how to Update Data from MySQL Database using PHP MySQLi.

Database Structure:

Database name: tutorial
Table Name: test
SQL code for table test Creation:
CREATE TABLE IF NOT EXISTS `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(150) NOT NULL,
  `email` varchar(150) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;


Dumping Data for Table test
INSERT INTO `test` (`id`, `name`, `email`) VALUES
(12, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(11, 'Krishna', 'krishna@mail.com'),
(3, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(4, 'ANJAN', 'KumarANjan@gmail.com'),
(5, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(7, 'Hrithik', 'Hrithik@gmail.com'),
(8, 'Modhu Bonik', 'Modhu360@gmail.com'),
(9, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(13, 'Rupa', 'Rupa@hotmail.com'),
(14, 'ANJAN', 'Kumar@gmail.com'),
(15, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(16, 'Krishna', 'krishna@mail.com'),
(17, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(18, 'ANJAN', 'KumarANjan@gmail.com'),
(19, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(20, 'Hrithik', 'Hrithik@gmail.com'),
(21, 'Modhu Bonik', 'Modhu360@gmail.com'),
(22, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(23, 'Rupa', 'Rupa@hotmail.com'),
(24, 'ANJAN', 'Kumar@gmail.com'),
(25, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(26, 'ANJANBD60', 'Anjankumardhali6@gmail.comss'),
(27, 'Priyoshi', 'Priyoshi@gmail.com'),
(28, 'Rupom Chakma', 'Rupam@gmail.com'),
(29, 'Monisha Roy', 'Monisharoy@gmail.com');

Directory or Folder and files Structure:

Create 4 files in any directory under htdocs folder.
1.db_config.php
2.index.php [Displaying Data with Update Button]
3.update.php
4.style.css
php mysqli update data from database
php mysqli update data from database folder Structure

Codes for db_config.php

<?php
$host = "localhost"; //Host name
$username = "root";  //MySQL Username .'root' is default
$password = "";  //MySQL Password
$db_name="tutorial"; //Database Name

// Create connection passing host,username,password and db_name 
$conn = new mysqli($host, $username, $password,$db_name);

// Checking connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

Codes for index.php

<link rel="stylesheet" href="style.css" type="text/css"/>
<?php
include("db_config.php");;
$sql = "SELECT id,name, email FROM test ORDER by id ASC ";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table>";
 echo "<tr>";
 echo "<th>ID</th>";
 echo "<th>Name</th>";
 echo "<th>Email</th>";
 echo "<th>Action</th>";
 echo "</tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
     echo "<tr>";
      echo "<td>$row[id]</td>";
   echo "<td>$row[name]</td>";
   echo "<td>$row[email]</td>";
   echo "<td><a href=update.php?id=$row[id]>Update</a></td>";
  echo "</tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>

Codes for update.php

<?php
include('db_config.php');

  $id=$_GET['id'];
//Getting value Passing ID
$sql = "SELECT name, email FROM test WHERE id=$id ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
    // Output Data of Each row
    while($row = $result->fetch_assoc()) {
   echo "<form method='POST' value=''>";
      echo "<input type='hidden' name='newid' value='$id'/>";
   echo "Name:<br/>  <input type='text' name='NewName' value='$row[name]'/><br/>";
   echo "Email:<br/>  <input type='text' name='NewEmail' value='$row[email]'/><br/>";
   echo "<input type='submit' name='submit' value='Update'/>";
   echo "</form>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>

<?php
include('db_config.php');
//Connecting to Database and Server
  $conn = new mysqli($host, $username, $password,$db_name);
//Getting Value from Index page Accroding to ID
if(isset($_POST['submit'])){
    $id=$_POST['newid'];
    $name=$_POST['NewName'];
 $email=$_POST['NewEmail'];
 // SQL to Update record form test table passing ID,Name and Email
   $sql = "UPDATE test SET name='$name',email='$email' WHERE id=$id";

 if ($conn->query($sql) === TRUE) {
    header("location:index.php");
 }else{
     echo "Error deleting record: " . $conn->error;
  }
$conn->close();
}
?>

Codes for style.css

table th, td{
   border: 1px solid blue;
   text-align:left;
}
table th{
 padding:12px;
 background:lightgreen;
 font-weight:bold;
 font-size:20px;
 text-align:center;
 
}
th,td{
 padding:8px;
}
table{
 border-collapse:collapse;
 width:50%;
}
table td a{
 background-color: #4CAF50;
    border: none;
    color: white;
    padding: 7px 12px;
    text-decoration: none;
    margin: 4px 2px;
    cursor: pointer;
 
}
tr:nth-child(even){background:#808080}

 

If you Like This article Please Like and Share With Your Friends.
Keep Visiting for More Codes.Thank you.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.