Insert Multiple Rows into MySQL with PHP Using Foreach Arrays


Insert Multiple Rows into MySQL with PHP Using Foreach Arrays

In this tutorial, we are going to learn how to insert multiple rows into MySQL with PHP using foreach arrays. To insert multiple form data, we need to generate multiple form rows and we will do it by using the click function in jquery to generate the rows on clicking the button multiple times, and on the other hand, we will use the remove() function to remove the desired row. Below I have explained the step-by-step process.

Insert Multiple Rows into MySQL with PHP Using Foreach Arrays

Inserting multiple rows into MySQL clicking Add new row button using jquery before submitting the form

  • When we click add new row button the first time, then we will get the length of the row by using the class of the field ‘SL No’ from the class attribute of SL No field.
  • Now, we will increase the length of the rows by adding 1 with parseInt(1) and store in a variable i.
  • Now, we will add the next row using the append() function in jquery and use the length variable i with all the remaining fields of the form.
  • We will also remove the added rows with the help of the remove function as mentioned in the code given below.

Submitting the multiple form fields in the PHP scripts

  • Here, we will receive the submitted rows using the field sl no.
  • Now, we will check the total rows submitted in the for loop with the help of the count() function.
  • We will store the remaining fields in the array variable of the for loop and insert the field values in the table of the database.

Here I am using a table name ‘student’ to insert the student information.

Also Read, How to append data to the dropdown list using jquery ajax and php

DDL information of the table ‘student’
——————————–

CREATE TABLE `student` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `student_name` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `phone_no` varchar(225) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `age` int(20) DEFAULT NULL,
  `date_of_birth` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

Complete Code:-

<?php
//session_start();
include('connect.php');
if(isset($_POST['submit'])){
for($i=0;$i<count($_POST['slno']);$i++){
		$student_name = $_POST['student_name'][$i];
		$phone_no = $_POST['phone_no'][$i];
		$age = $_POST['age'][$i];
		$date_of_birth = $_POST['date_of_birth'][$i];
		if($student_name!=='' && $phone_no!=='' && $age!=='' && $date_of_birth!==''){
	$sql="INSERT INTO student(student_name,phone_no,age,date_of_birth)VALUES('$student_name','$phone_no','$age','$date_of_birth')";
			$stmt=$con->prepare($sql);
			$stmt->execute();
		    //echo '<div class="alert alert-success" role="alert">Submitted Successfully</div>';
		}
		else{
			
			echo '<div class="alert alert-danger" role="alert">Error Submitting in Data</div>';
		}
	}
	echo "<script type='text/javascript'>";
		echo "alert('Submitted successfully')";
		echo "</script>";
}
?>
<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:80%;
	height:30%;
	padding:20px;
}
</style>
</head>
<body>
<div class="container">
<h3 align="center"><u>Inserting Multiple Rows in PHP</u></h3>
<br/><br/><br/>
	<form class="form-horizontal" action="#" method="post">
	<div class="row">
		<div class="col-sm-1">
	      <label for="Age">Sl No:</label>
	      <input type="text" class="form-control sl" name="slno[]" id="slno" value="1" readonly="">
	    </div>
	    
	    <div class="col-sm-3">
	      <label for="Student Name">Student Name:</label>
	      <input type="text" class="form-control" name="student_name[]" id="st_name" placeholder="Enter Student Name">
	    </div>
	    
	    <div class="col-sm-3">
	     <label for="Phone">Phone No*:</label>
	      <input type="text" class="form-control" name="phone_no[]" id="pn" placeholder="Enter Phone No">
	    </div>
	    
	    <div class="col-sm-1">
	      <label for="Age">Age:</label>
	      <input type="text" class="form-control" id="age" name="age[]" placeholder="Enter Age">
	    </div>
		
		<div class="col-sm-3">
	     <label for="DateofBirth">Date of Birth:</label>
	     	<input type="date" id="dob" name="date_of_birth[]" class="form-control"/>
	    </div>
	    
		</div><br/>
		<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" name="submit" class="btn btn-info pull-left">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="row"><div class="col-sm-1"><label for="Age">Sl No:</label><input type="text" class="form-control sl" name="slno[]" value="'+i+'" readonly=""></div><div class="col-sm-3"><label for="Student Name">Student Name:</label><input type="text" class="form-control" name="student_name[]" id="st_name'+i+'" placeholder="Enter Student Name"></div><div class="col-sm-3"><label for="Phone">Phone No*:</label><input type="text" class="form-control" name="phone_no[]" id="pn'+i+'" placeholder="Enter Phone No"></div><div class="col-sm-1"><label for="Age">Age:</label><input type="text" class="form-control" id="age'+i+'" name="age[]" placeholder="Enter Age"></div><div class="col-sm-3"><label for="DateofBirth">Date of Birth:</label><input type="date" id="dob'+i+'" name="date_of_birth[]" class="form-control"/></div><input type="button" class="btnRemove btn-danger" value="Remove"/></div><br>');
		
		});
	
	// 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.

Also Read, How to insert ajax result in a text field


CONCLUSION:- I hope this article will help you to insert multiple rows into MySQL with PHP using foreach arrays. If you have any doubt then please leave your comment below.     


5 thoughts on “Insert Multiple Rows into MySQL with PHP Using Foreach Arrays”

  1. I do not even know the way I stopped up here, but I thought this put up was
    once great. I don’t recognise who you’re however definitely you’re going to
    a well-known blogger if you happen to are not already. Cheers!

    Reply

Leave a Comment

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

sabung ayam online

THOR311

parlay

RATU311

KUPU178

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 DEMO SLOT

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

parlay

HOKI311

THOR311

Thor311 Aplikasi

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

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

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311