PHP MySQL Update DataBase - Web Development and Web Design Codes

Latest

Monday, April 17, 2017

PHP MySQL Update DataBase

Update Data from MySQL using PHP

It's very easy and simple to Update data from mysql database using php.
PHP MySQL Update DataBase
PHP MySQL Update DataBase
in this step we are going to learn how to update data from database using php..
every beginners learner should learn php mysql update..if you don't know how to update mysql data please learn the below step....

Overview and Structure:

You will need 3 files. 
1. connect.php [To Established Connection with Database]
2. index.php [Displaying Data with 'Update' Button]
3. update.php  [Updating Data from database according to ID]
Save all files in same folder.

Database Structure:

PHP MySQL Update DataBase
PHP MySQL Update DataBase

 Database Name: tutorial
Table Name: test
Creating Table "test"
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=15 DEFAULT CHARSET=latin1;

Dumping data for table 'test':
INSERT INTO `test` (`id`, `name`, `email`) VALUES
(NULL, 'ANJANBD', 'Anjankumardhali6@gmail.com'),
(NULL, 'Krishna', 'krishna@mail.com'),
(NULL, 'Rupam Mondal', 'RupomMondal@gmail.com'),
(NULL, 'ANJAN', 'KumarANjan@gmail.com'),
(NULL, 'Biswas Shiuli', 'ShiuliBiswas@gmail.com'),
(NULL, 'Hrithik', 'Hrithik@gmail.com'),
(NULL, 'Modhu Bonik', 'Modhu360@gmail.com'),
(NULL, 'Rupam Mondal Joy', 'RupomMondal@gmail.com'),
(NULL, 'Rupa', 'Rupa@hotmail.com'),
(NULL, 'ANJAN', 'Kumar@gmail.com');

connect.php
You have to create connect.php file to create connection between your server and database..
<?php
$host="localhost"; //Host Name 
$username="root"; //MySQL UserName
$password=""; //MySQL PassWord
$dbname="tutorial"; //Database Name
//Create Connection
$conn=new mysqli($host,$username,$password,$dbname);
if($conn->connect_error){
 echo("Connection Failed:".$conn->connect_error);
}
?>

index.php
You have to create index.php file to display data from database with Update button..when user click on update button will redirect to update.php page
<html>
<head>
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
th, td {
    padding: 5px;
}
th {
    text-align: left;
}
</style>
</head>

<body>

<table style="width:50%">
 
 
  <tr>
    <th>ID</th>
 <th>Name</th>
 <th>Email</th>
 <th>Action</th>
  </tr>
  
  <?php 
   include("connect.php");
  $result=mysqli_query($conn,"SELECT*FROM test"); //Selecting Data From "test" Table to Display
//Fetching Data From Table to Display
while($row=$result->fetch_array()){?> 

 <tr>
     <td><?php echo $row['id'];?></td>
  <td><?php echo $row['name'];?></td>
  <td><?php echo $row['email'];?></td>
  <td><a href="update.php?id=<?php echo $row['id'];?>">Update</a></td>
 </tr>
   
  <?php } ?>
</table>

</body>
</html>
update.php
You have to create update.php file to displaying selected data by id with update button..when user click on update button will redirect to index page if data data successfully updated or get error message if failed to update
PHP MySQL Update DataBase
PHP MySQL Update DataBase

<?php
include("connect.php");
//Getting ID Values url
   $id=$_GET['id'];
//Displaying Data form Database by Passing ID
   $result=mysqli_query($conn,"SELECT*FROM test WHERE id='$id'");
   $row=$result->fetch_assoc();
   ?>
  <form method="POST" action="">
  <table>
      <tr>
   <input type="hidden" name="id" value="<?php echo $row['id'];?>"/>
       <td>Name:</td>
    <td><input type="text" name="Newname" value="<?php echo $row['name'];?>"/></td>
   </tr>
   <tr>
       <td>Email:</td>
    <td><input type="text" name="Newemail" value="<?php echo $row['email'];?>"/></td>
   </tr>
   <tr>
       <td></td>
    <td><input type="submit" name="update" value="Update"/></td>
   </tr>
  </table>
</form>
 <?php
 //We will Do Something if User Press or Click on The Update Button
 if(isset($_POST['update'])){
  $id=$_POST['id'];
  $name=$_POST['Newname'];
  $email=$_POST['Newemail'];
  //Updating "test" Table Passing Newname and Newemail by ID
  $update=mysqli_query($conn,"UPDATE test SET name='$name',email='$email' WHERE id='$id'");
  if($update){
   header('location:index.php');
  }else{
   echo "Sorry Failed to Update";
  }
 }
 ?>

That's it friends how to update data from mysql database using php ..I hope you Enjoy This article.Please Like and Share with your Friends.
and Keep Visiting for More Tutorials.Thank you.

No comments:

Post a Comment

Thank You for Your Comment

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