Learn PHP,HTML,CSS,Javascript,MySQL,Jquery

Sunday, 25 October 2020

How to insert array data into mysql database in php

 Insert Array Data into Mysql Database PHP



Inserting array type data into mysql database table using php programming language is very important thing to learn for all php programmers. It is mostly used in very big applications to develop web pages. You can insert multiple arrays or a single array field with some other fields. Foreach loop will be used to insert array type data into database by using php. 

Table of Content

1- Create HTML Form
2- MySQL Database Create a Table
3- PHP Code to Insert Array Data

1- Create HTML Form

First step is to create a new php file within this file write html code and create html form.Within this html form you have to write array input fields. Array input fields in html are written by adding [] braces with name attribute value and same name in each input field of array type. for example name="question[]". 
HTML FORM Code Example

  <!--DOCTYPE html-->
<html>
<head>
<title>Insert Array Data</title>
 <link href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.0/css/bulma.min.css" rel="stylesheet" media="all" />
  <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" media="all" />
<script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
</head>
<body>
<section class="her is-fullheight">
<div class="container">
<form action="#" method="post">
<div class="box">
<h1 class="title">Insert Array Type Data Into MySQL Table</h1>
<div class="column">
<!--php echo $output;?-->
<div class="column">
<label class="label">Question#1<label>
<input type="text" class="input is-large is-success" name="txt_question[]" />
</div>
<div class="column">
<label class="label">Question#2<label>
<input type="text" class="input is-large is-success" name="txt_question[]" />
</div>
<div class="column">
<label class="label">Question#3<label>
<input type="text" class="input is-large is-success" name="txt_question[]" />
</div>
<div class="column">
<label class="label">Question#4<label>
<input type="text" class="input is-large is-success" name="txt_question[]" />
</div>
<div class="column">
<label class="label">Question#5<label>
<input type="text" class="input is-large is-success" name="txt_question[]" />
</div>
<div class="column is-4 is-offset-4">
<input type="submit" name="btn_submit" class="button is-success is-large is-fullwidth" value="Save Questions" />
</div>
</div>
</div>
</form>
</div>
</section>
</body>
</html>
  

2- MySQL Database Create a Table 

Open phpmyadmin in new tab of browser. Select an existing database or create new database. Within this database create a table in which you want to insert array type data. 

3- PHP Code to Insert Array Data

Here you have to write php code of button click you can write this code within same page or write code in a separate file then include this file. Initialize all variables then start a foreach loop for array field variable. Within this foreach loop write insert query of mysql to insert data into database table. 
PHP code to insert array type data

<!-- php
$output="";
if(isset($_POST["btn_submit"]))
{
$conn=mysqli_connect("localhost","root","i7biD7XBRMEwwFVE","test") or die("Failed to connect with database");
$questions=$_POST["txt_question"];
$date=date("Y-m-d H:i:s");
foreach($questions as $item)
{
	$sql="INSERT INTO `question_list`( `question`, `datetime`) VALUES ('$item','$date')";
	$result=mysqli_query($conn,$sql);	
}
if($result)
	{
		$output='<div class="column box notification is-danger"-->Form submitted successfully</div>';
	} else {
		$output='<div class="column box notification is-danger">Failed to submit form</div>';
	}
}
? >

No comments:

Post a Comment