Inline insert

Inline insert using php and Mysql

Source code

index.php

								
									<?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

Comments

John Simith

This is clean code I ever seen in my life.

Happy Dorah

very nice code thank you code4berry

Don Williams

Thanks for teaching world

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