<?php
require_once("dbconnection.php");
error_reporting(0);
?>
<!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>Inline Insert in php</title>
<!--Bootstrap -->
<link rel="stylesheet" href="css/bootstrap.css" type="text/css">
</head>
<body>
<div class="container">
<div id="add-product">
<div class="pb-4">Add Product</div>
<table class="table table-bordered">
<tbody>
<tr style="background: #d1e6d6;">
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Description</strong></th>
<th style="text-align:right;">>strong>Price</strong></th>
</tr>
<tr>
<td contentEditable="true" data-id="product_name"></td>
<td contentEditable="true" data-id="product_code"></td>
<td contentEditable="true" data-id="product_desc"></td>
<td contentEditable="true" data-id="product_price" style="text-align:right;"></td>
</tr>
</tbody>
</table>
<div id="btnSaveAction" class="btn btn-sm btn-primary ">Save to Database</div>
</div>
<div id="list-product">
<div class="txt-heading pb-2" align="center"><h2> Products</h2></div>
<table class="table table-bordered" style="background-color:#F0F0F0;">
<tbody id="ajax-response">
<tr style="background: #d1e6d6;">
<th><strong>Name</strong></th>
<th><strong>Code</strong></th>
<th><strong>Description</strong></th>
<th style="text-align:right;"><strong>Price</strong></th>
</tr>
<?php
$sql = "SELECT * from tbl_product 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 $result)
{
?>
<tr>
<td><?php echo htmlentities($result->product_name);?></td>
<td><?php echo htmlentities($result->product_code);?></td>
<td><?php echo htmlentities($result->product_desc);?></td>
<td style="text-align:right;"><?php echo htmlentities($result->product_price);?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
</div>
<script src="js/jquery.js"></script>
<script>
$("#btnSaveAction").on("click",function(){
insert = ""
$("td[contentEditable='true']").each(function(){
if($(this).text() != "") {
if(insert != "") {
insert += "&";
}
insert += $(this).data('id')+"="+$(this).text();
}
});
if(insert!="") {
$.ajax({
url: "insert-row.php",
type: "POST",
data:insert,
success: function(response){
$("#ajax-response").append(response);
$("td[contentEditable='true']").text("");
}
});
}
});
</script>
</div>
</body>
</html>
insert-row.php
<?php
require_once("dbconnection.php");
if(!empty($_POST["product_name"]))
{
$product=$_POST['product_name'];
$code=$_POST['product_code'];
$desc=$_POST['product_desc'];
$price=$_POST['product_price'];
$sql="insert into tbl_product (product_name,product_code,product_desc,product_price)values(:product,:code,:desc,:price)";
$query=$dbh->prepare($sql);
$query->bindParam(':product',$product,PDO::PARAM_STR);
$query->bindParam(':code',$code,PDO::PARAM_STR);
$query->bindParam(':desc',$desc,PDO::PARAM_STR);
$query->bindParam(':price',$price,PDO::PARAM_STR);
$query->execute();
$LastInsertId=$dbh->lastInsertId();
if ($LastInsertId>0) {
echo '<script>alert("Product has been added.")</script>';
echo "<script>window.location.href = 'index.php'</script>";
}
else
{
echo '<script>alert("Something Went Wrong. Please try again")</script>';
}
}
?>
dbconnection.php
<?php
// DB credentials.
define('DB_HOST','localhost');
define('DB_USER','root');
define('DB_PASS','');
define('DB_NAME','inlinedb');
// 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());
}
?>
Download