Insert Multiple Rows Using the Same id in PHP


Insert Multiple Rows Using the Same id in PHP

In this tutorial, we will learn how to insert multiple rows using the same id in PHP. For example, we have some fruit items like orange, mango, banana, etc. Now, we will consider the fruit as an item category and fruit-related items as a fruit subcategory. We will insert all the subcategory items using the parent category inside the MySQL database. I hope it’s clear.

Next, we will select the parent category as shown in the above image and create multiple subcategories with the help of jquery so that we can insert the multiple subcategories under one category.

Now, we will create multiple rows for subcategories with jquery. If you don’t know then read also,

how to create multiple rows using jquery.

Insert multiple rows using the same id into the database after the form submission

  • When we submit the form data then the parent category will be inserted into a table name ‘items’ in the database and the subcategories will be inserted into another table ‘item_details’ using the category id from the ‘items’ table.

Steps of inserting the subcategories using the parent category id

  • First of all, we have to create two tables in the database.
  • The first table contains the row including the information of the parent category.
  • The second table contains the rows including the information corresponding to the parent id of the first table.
  • Now, we will check the submitted category with the help of rowCount() function whether a row exists or not. If there is any row then we will take the category id using the lastInsertId() function.
  • We will count the total subcategory submitted with the help of the hidden id mentioned in the form.
  • We will run a for loop to insert the subcategories using the last inserted id into the ‘item_details’ table.

In order to do this, we have to create two tables, the first table contains only the category name, and the other table contains the subcategory name and the category id.

Here, I am using two tables, items and item_details.

DDL information of the table ‘items’

CREATE TABLE items (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
item_name varchar(225) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4

DDL information of the table ‘item_details’

CREATE TABLE item_details (
id int(10) unsigned NOT NULL AUTO_INCREMENT,
item_id int(10) DEFAULT NULL,
item_subcategory varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Complete Code:-

<?php
//session_start();
include('connect.php');
if(isset($_POST['save']))
{
	//insert item_name into items table
	$item_name = $_POST['item_name'];
	$sql="INSERT INTO items(item_name)VALUES('$item_name')";
	$stmt=$con->prepare($sql);
	$stmt->execute();
	//get the item id by lastinsertid function
	if($stmt->rowCount()>0)
	{
		$lastid = $con->lastInsertId();
		for($i=0;$i<sizeof($_POST['serial']);$i++)
		{
			$item_id = $lastid;
			$item_subcategory = $_POST['item_subcategory'][$i];
		
		$sql1="INSERT INTO item_details(item_id,item_subcategory)VALUES('$item_id','$item_subcategory')";
			$stmt1=$con->prepare($sql1);
			$stmt1->execute();
		}

	}
}
?>
<html>
<head>
<title>ajax example</title>
<link rel="stylesheet" href="bootstrap.css" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap-theme.css" crossorigin="anonymous">

<style>
.container{
	width:50%;
	height:30%;
	padding:20px;
	
}

</style>
</head>
<body>
	<div class="container">
		<h3 align="center"><u>INSERT MULTIPLE ROW INTO DATABASE USING SINGLE ID</u></h3>
		<br/><br/><br/>
			<form class="form-horizontal" action="#" method="post">
				<div id="newuser"></div>
					  <div class="form-group">
					    <label class="control-label col-sm-2" for="item">Items*:</label>

						    <div class="col-sm-6">
						      <select class="form-control" name="item_name" id="it" required="">
						      	<option value="0">--Please Select--</option> 
								   <option value="FRUITS">FRUITS</option> 
								   <option value="ANOTHER FOOD ITEMS">ANOTHER FOOD ITEMS</option> 
						      </select>
						    </div>
						    
					  </div>
					  <br/>
					  <br>
					<h4><u>Insert Item Subcategory</u></h4>
					<br>
					<div class="form-group">
					    <label class="control-label col-sm-2" for="item">Item Subcategory*:</label>
	
						    <div class="col-sm-6">
						      <input type="text" class="form-control" name="item_subcategory[]" id="itsubcat">
						    </div>
						    
					  </div>
					  <input type="hidden" name="serial[]" class="sl" id="hd" value="1">
					<div id="next"></div>
			   		<br/>
					<button type="button" name="addrow" id="addrow" class="btn btn-success pull-right">Add New Row</button>
					<br><br>
					  <button type="submit" class="btn btn-primary pull-right" name="save">Submit</button>
					  
			</form>
			
	</div>
	
<script src="jquery-3.2.1.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
$('#addrow').click(function(){
		
		var length = $('.sl').length;
		var i   = parseInt(length)+parseInt(1);
		var newrow = $('#next').append('<div class="form-group"><input type="hidden" name="serial[]" class="sl" value="'+i+'"><label class="control-label col-sm-2" for="item">Item Subcategory*:</label><div class="col-sm-8"><input type="text" class="form-control" name="item_subcategory[]" id="itsubcat'+i+'"></div><input type="button" class="btnRemove btn-danger col-sm-2" value="Remove"/></div>');
		
		});
	
	// Removing event here
  $('body').on('click','.btnRemove',function() {
       $(this).closest('div').remove()

  });
</script>

</body>
</html>

NOTE*

Download the bootstrap CSS and js files from google and include the path of the files in the href attribute of link tag and src attribute of the script tag respectively.

CONCLUSION:- I hope this article will help you to insert multiple rows using the same id in PHP. If you have any doubt then please leave your comment below.


Leave a Comment

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

RATU311

RATU311

THOR311

parlay

CERI188

HOKI311

RATU311

RATU311

judi bola online

Mix Parlay

THOR311

Ceri188

Thor311 Game Online

Thor311 Aplikasi

LINK CERI188

RATU311 NONTON BOLA

RATU311

HOKI311 Link Alternatif

CERI188 Link RTP LIVE

THOR311

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

THOR311 LOGIN

THOR311

THOR311

THOR311

RATU311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

RATU311

THOR311

THOR311

HOKI311 Link Apk Resmi

RATU311

KUPU178

RATU311

RATU311

RATU311

CERI188

CERI188 Situs Spaceman Terbaik 2026

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188 Live Casino

HOKI311 Lapak Arena Slot

RATU311 judi bola

CERI188 Live Casino

HOKI311 Link Spaceman Online

RATU311

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

KUPU178

KUPU178

KUPU178

HOKI311 TEMBAK IKAN ONLINE

RATU311

CERI188 PANDUAN MAXWIN SLOT ONLINE

RATU311

RATU311

RATU311

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

THOR311

Ceri188 Game Online

CERI188

link login CERI188

THOR311

link login CERI188

CERI188

link login CERI188

link login CERI188

link daftar CERI188

THOR311

HOKI311

HOKI311

CERI188

CERI188

CERI188

parlay

THOR311

RATU311

RATU311

HOKI311

HOKI311

parlay

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

RATU311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

situs bola

THOR311

slot gacor

slot gacor

yakuza303

slot gacor

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

mix parlay

judi bola

judi bola

mix parlay

parlay

judi bola

sv388

judi bola

judi bola

judi bola

parlay

THOR311