Update Records in mysql datebase

Update Records in Mysql :


It is easy to update records in mysql database using html forms.After creating insert record in mysql we can easily update records but make sure while using update you have to pass id of a particular record from previous script like the url will take particular id to that script which will take part in updating the particular record.In this case display.php is script that will display record and when user will click on update than display.php file will pass the id of particular record that user clicked using the query string or url and than update.php file will receive that id and select the particular record and display all the data into form and when user will click on update button in upadate.php than it will take action and update the record and if record updation is successfull than again it will redirect the user to display.php using header function if failed to update record it will display a failed message to user. If you are unable to understand make sure you go throung "Insert records" and "display records"




Logic Behind Updating Record :


The main logic behind updation record is update command of sql as follows

Update  tablename set tablefield = value where id =  --


Sql for database

i am using 'php_operations' as database name and crud as table name so create the database with name of php_operations and put the following sql in sql tab of mysql as instructed in video.



CREATE TABLE `crud` (
`id` int(11) NOT NULL,
`firstName` varchar(255) NOT NULL,
`lastName` varchar(255) NOT NULL,
`gender` varchar(255) NOT NULL,
`age` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `crud` (`id`, `firstName`, `lastName`, `gender`, `age`, `email`) VALUES
(1, 'jeff', 'morgan', 'male', '42', 'jeff@gmail.com'),
(2, 'stenly', 'uba', 'female', '21', 'stenly@gmail.com'),
(3, 'pablo', 'morgan', 'male', '98', 'pablo@gmail.com'),
(4, 'susan', 'arya', 'female', '22', 'susan@yahoo.com');
ALTER TABLE `crud` ADD PRIMARY KEY (`id`);

ALTER TABLE `crud`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;


The file Display.php


For updating record first we need to display the records with a update button so here is the code that will send data to update.php file for communication



<!DOCTYPE html>
<html>
<head>
 <title>Simple CRUD Application - READ Operation</title>
 <!-- Latest compiled and minified CSS -->
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
<body>
 <div class="container">
 <div class="row">
 <h1><center>Fetch Operation using php - crud</center></h1>
 <table class="table ">
 <thead> <tr>
 <th>#</th>
 <th>Full Name</th>
 <th>E-Mail</th>
 <th>Age</th>
 <th>Gender</th>
 <th>Update Record</th>
 <th>Delete Record</th>
 </tr>
 </thead>
 <tbody>

<?php
 require_once('connection.php');
 $query = "SELECT * FROM `crud`";
 $result = mysqli_query($connection, $query);
 while($row = mysqli_fetch_assoc($result)){
?>
 <tr>
 <th scope="row"><?php echo $row['id']; ?></th>
 <td><?php echo $row['firstName'] . " " . $row['lastName']; ?></td>
 <td><?php echo $row['email']; ?></td>
 <td><?php echo $row['gender']; ?></td>
 <td><?php echo $row['age']; ?></td>
 <td>
 <a href="update.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-edit" aria-hidden="true"></span></a>
 </td>
 <td>
 <a href="delete.php?id=<?php echo $row['id']; ?>"><span class="glyphicon glyphicon-remove" aria-hidden="true"></span></a>
 </td>
 </tr>
<?php } ?>
 </tbody>
 </table>
 </div>
 </div>
</body>
</html>



Update.php for updatetion of reocrds

This file will update the records in database when user will click on the Update button by changing records in form and this will redirect user back to display.php file for showing the updated record.


<!DOCTYPE html>
<html>
<head>
 <title>Update Operations in php</title>
 <!-- Latest compiled and minified bootstrap CSS -->
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
</head>
<body>
 <div class="container">
<?php
 error_reporting(0);
 require_once('connection.php');
 $id = $_GET['id'];
 $queryl = "SELECT * FROM `crud` WHERE id=$id";
 $result1 = mysqli_query($connection, $queryl);
 $row = mysqli_fetch_assoc($result1);
?>
 <div class="row">
  <form method="post" class="form-horizontal col-md-6 col-md-offset-3">
  <center><h2>Execute crud operations</h2></center>
<br/><hr/>
  <div class="form-group">
 <label for="item1" class="col-sm-3 control-label">First Name</label>
 <div class="col-sm-9">
 <input type="text" name="f_name" class="form-control" id="item1" value="<?php echo $row['firstName']; ?>" placeholder="First Name" />
 </div>
 </div>
 <div class="form-group">
 <label for="item2" class="col-sm-3 control-label">Last Name</label>
 <div class="col-sm-9">
 <input type="text" name="l_name" class="form-control" id="item2" value="<?php echo $row['lastName']; ?>" placeholder="Enter Your Last Name" />
 </div>
 </div>
 <div class="form-group">
 <label for="item3" class="col-sm-3 control-label">E-Mail</label>
 <div class="col-sm-9">
 <input type="email" name="email" class="form-control" id="item3" value="<?php echo $row['email']; ?>" placeholder="Your Email" />
 </div>
 </div>
 <div class="form-group" class="radio">
 <label for="input1" class="col-sm-3 control-label">Gender</label>
 <div class="col-sm-9">
 <label>
 <input type="radio" name="gender" id="radios1" value="male" <?php   if($row['gender'] == 'male'){ echo "checked";} ?>> Male </label>
 <label>
 <input type="radio" name="gender" id="optionsRadios1" value="female" <?php if($row['gender'] == 'female'){ echo "checked";} ?>>feMale
 </label>
 </div>
 </div>
 <div class="form-group">
 <label for="item4" class="col-sm-3 control-label">Age</label>
 <div class="col-sm-9">
 <input type="text" name="age" class="form-control" id="item4" value="<?php echo $row['age']; ?>" placeholder="Your Age" />
 </div>
 </div>
 <input type="submit" class="btn btn-danger col-md-2 col-md-offset-10" value="Update" />
 </form>
 </div>
 </div>
</body>
</html>
<?php
 if(isset($_POST) & !empty($_POST)){
 $f_name = $_POST['f_name'];
 $l_name = $_POST['l_name'];
 $email = $_POST['email'];
 $gender = $_POST['gender'];
 $age = $_POST['age'];

 $query2 = "UPDATE `crud` SET firstName='$f_name', lastName='$l_name', gender='$gender', age=$age, email='$email' WHERE id=$id";
 $result2 = mysqli_query($connection, $query2);
 if($result2){
 header('location: display.php');
 }else{
 $faildMsg = "Data can't be updated try again.";
 }
}
?>
 <?php if(isset($faildMsg)){ ?><div class="alert alert-danger" role="alert"><center> <?php echo $faildMsg; ?></center></div><?php?>





Feel free to comment below if have any issue regarding code.