PHP Insert Multiple Records Into MySQL
![]() |
PHP Insert Multiple Records Into MySQL--CodeNair.com |
Hi Guys in This Lesson we are going to learn PHP Insert Multiple Records Into MySQL. follow the below example and learn insert multiple records using php
=>Database and Table:
Database Name: test
Table Name : company
SQL Codes for Creating table company
CREATE TABLE IF NOT EXISTS `company` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(100) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Files:
Create 3 files
1.index.php
2.insert.php
3.style.css
1. index.html
<html> <head> <title>Inserting Multiple Rows using PHP MySQL</title> <link rel="stylesheet" type="text/css" href="style.css"/> </head> <h2 align="center">Inserting Multiple Rows using PHP MySQL</h2> <form method="POST" action="insert.php"> <table> <tr> <td>Name 1:</td> <td><input type="text" name="name[]" required/> </td> </tr> <tr> <td>Name 2:</td> <td><input type="text" name="name[]" required/> </td> </tr> <tr> <td>Name 3:</td> <td><input type="text" name="name[]" required/> </td> </tr> <tr> <td>Name 4:</td> <td><input type="text" name="name[]" required/> </td> </tr> <tr> <td></td> <td> <input type="submit" name="submit" value="submit"/> </td> </tr> </table> </form> <p align="center"> <?php if(isset($_GET['msg'])){ echo 'Your Data Successfully Inserted'; } ?> </p> </html>2. insert.php
<?php //Creating Database Connection $conn=new mysqli('localhost','root','','test'); //Getting Comapny Name from form $name=$_POST['name']; foreach($name as $key){ //Inserting Data into compnay $result=mysqli_query($conn,"INSERT INTO company (name)VALUES('$key')"); } if($result==TRUE){ header('location:index.php?msg='.urlencode('Your Data Successfully Inserted')); }else{ echo "Failed To INSERT Data"; } ?>3. style.css
table{ margin:auto; width:500px; box-shadow:2px 2px 4px 2px green; } input[type=text]{ width:250px; padding:6px; border-radius:4px; } input[type=submit]{ background:black; color:white; font-weight:bold; padding:6px; border-radius:4px; cursor:pointer; }That's it Guys PHP Insert Multiple Records Into MySQL.
Keep visiting for more codes. Please leave your comment's .Thank you for Visiting...
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.