Edit Mysql data in modal

How to edit Mysql data in modal using php

Source code

config.php

								
									<?php 
									// DB credentials.
									define('DB_HOST','localhost');
									define('DB_USER','root');
									define('DB_PASS','');
									define('DB_NAME','modaldb');
									// Establish database connection.
									try
									{
									$dbh = new PDO("mysql:host=".DB_HOST.";dbname=".DB_NAME,DB_USER, DB_PASS,array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'"));
									}
									catch (PDOException $e)
									{
									exit("Error: " . $e->getMessage());
									}
									?>
								
							

index.php

								
									<?php
									session_start();
									error_reporting(0);
									include "config.php";
									?>

									<!DOCTYPE HTML>
									<html lang="en">
									<head>
										<meta charset="utf-8">
										<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
										<title>How to Edit data in modal</title>
										<!--Bootstrap -->
										<link rel="stylesheet" href="css/bootstrap.css" type="text/css">
										<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css">
									</head>
									<body>
										<div class="container">
											<div> </div>
											<h3>How to edit Mysql data in modal</h3>
											<div class="row mt-6">
												
												<table id="" class="table table-bordered">
													<thead>
														<tr>
															<th class="text-center">No</th>
															<th>Product Name</th>
															<th class="text-center">Product Category</th>
															<th class="text-center"> Product Price</th>
															<th class="text-center">Posting Date</th>
															<th class=" text-center">Action</th>
														</tr>
													</thead>

													<tbody>
														<?php
														$sql="SELECT * from tblproducts ORDER BY id DESC";
														$query = $dbh -> prepare($sql);
														$query->execute();
														$results=$query->fetchAll(PDO::FETCH_OBJ);
														$cnt=1;
														if($query->rowCount() > 0)
														{
															foreach($results as $row)
															{ 
																?>
																<tr>
																	<td class="text-center"><?php echo htmlentities($cnt);?></td>
																	<td><?php  echo htmlentities($row->ProductName);?></td>
																	<td class="text-center"><?php  echo htmlentities($row->CategoryName);?></td>
																	<td class="text-center"><?php  echo htmlentities($row->ProductPrice);?></td>
																	<td class="text-center"><?php  echo htmlentities(date("d-m-Y", strtotime($row->PostingDate)));?></td>
																	<td class=" text-center">
																		<a href="#"  class=" edit_data4 btn btn-sm btn-primary" id="<?php echo  ($row->id); ?>" title="click to edit">Edit</a>
																	</td>
																</tr>
																<?php
																$cnt=$cnt+1;
															}
														} ?>
													</tbody>
												</table>
											</div>
											<!--  start  modal -->
											<div id="editData4" class="modal fade">
												<div class="modal-dialog modal-md">
													<div class="modal-content">
														<div class="modal-header">
															<h5 class="modal-title">Edit Product details</h5>
															<button type="button" class="close" data-dismiss="modal" aria-label="Close">
																<span aria-hidden="true">×</span>
															</button>
														</div>
														<div class="modal-body" id="info_update4">
															<?php @include("edit_product.php");?>
														</div>
														<div class="modal-footer ">
															<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
														</div>
														<!-- /.modal-content -->
													</div>
													<!-- /.modal-dialog -->
												</div>
												<!-- /.modal -->
											</div>
											<!--   end modal -->
										</div>>
										<!-- Loading Scripts -->
										<script src="js/jquery.min.js"></script>
										<script src="js/bootstrap-select.min.js"></script>
										<script src="js/bootstrap.min.js"></script>
										<script src="js/jquery.dataTables.min.js"></script>
										<script src="js/dataTables.bootstrap.min.js"></script>
										<script type="text/javascript">
											$(document).ready(function(){
												$(document).on('click','.edit_data4',function(){
													var edit_id4=$(this).attr('id');
													$.ajax({
														url:"edit_product.php",
														type:"post",
														data:{edit_id4:edit_id4},
														success:function(data){
															$("#info_update4").html(data);
															$("#editData4").modal('show');
														}
													});
												});
											});
										</script>
									</body>
									</html>
								
							

edit_product.php

								
									<?php
									session_start();
									error_reporting(0);
									include('config.php');
									if(isset($_POST['insert']))
									{
									    $eib= $_SESSION['editbid'];
									    $category=$_POST['category'];
									    $product=$_POST['product'];
									    $price=$_POST['price'];
									    $sql4="update tblproducts set CategoryName=:category,ProductName=:product,ProductPrice=:price where tblproducts.id=:eib";
									    $query=$dbh->prepare($sql4);
									    $query->bindParam(':category',$category,PDO::PARAM_STR);
									    $query->bindParam(':product',$product,PDO::PARAM_STR);
									    $query->bindParam(':price',$price,PDO::PARAM_STR);
									    $query->bindParam(':eib',$eib,PDO::PARAM_STR);
									    $query->execute();
									    if ($query->execute())
									    {
									        echo '<script>alert("updated successfuly")</script>';
									        echo "<script>window.location.href = 'index.php'</script>"; 
									    }else{
									        echo '<script>alert("update failed! try again later")</script>';
									    }
									}
									?>
									<div class="card-body">
									    <?php
									    $eid=$_POST['edit_id4'];
									    $sql2="SELECT * from tblproducts where id=:eid";
									    $query2 = $dbh -> prepare($sql2);
									    $query2-> bindParam(':eid', $eid, PDO::PARAM_STR);
									    $query2->execute();
									    $results=$query2->fetchAll(PDO::FETCH_OBJ);
									    if($query2->rowCount() > 0)
									    {
									        foreach($results as $row)
									        {
									            $_SESSION['editbid']=$row->id;
									            ?>

									            <form class="form-sample"  method="post" enctype="multipart/form-data">
									                <div class="row">
									                    <div class="form-group col-md-12">
									                        <label class="col-sm-12 pl-0 pr-0">Category Name</label>
									                        <div class="col-sm-12 pl-0 pr-0">
									                            <input type="text" name="category" id="category" class="form-control" value="<?php  echo $row->CategoryName;?>" required />
									                        </div>
									                    </div>
									                </div>
									                <div class="row">
									                    <div class="form-group col-md-12">
									                        <label class="col-sm-12 pl-0 pr-0">Product Name</label>
									                        <div class="col-sm-12 pl-0 pr-0">
									                            <input type="text" name="product" value="<?php  echo $row->ProductName;?>" class="form-control" required>
									                        </div>
									                    </div>
									                </div>
									                <div class="row">
									                    <div class="form-group col-md-12 ">
									                        <label class="col-sm-12 pl-0 pr-0">Product Price</label>
									                        <div class="col-sm-12 pl-0 pr-0">
									                            <input type="text" name="price" value="<?php  echo $row->ProductPrice;?>" class="form-control" required>
									                        </div>
									                    </div>
									                </div>
									                <button type="submit" name="insert" class="btn btn-primary btn-fw mr-2" style="float: left;">Update</button>
									            </form>
									            <?php 
									        }
									    } ?>
									</div>
								
							
Download

Comments

Cathy Pure

What a beautiful code this is ! thank you.

Leave a Comment:

You Might Also Like

hostel booking

Hostel booking management system
Learn More
.

car rental

Car rental management system in Php
Learn More
.

student details

Student details management system
Learn More
.

Tourism

Tourism management system in Php and Mysql
Learn More

Latest Tutorial

sweet alerts

How to delete table row using sweet alert2
Learn More

piechart

How to create piechart with Mysql data
Learn More

register and login

How to register and login in php
Learn More

edit data

How to edit Mysql data in modal using php
Learn More