This tutorial will guide and teach you on how to perform multiple insert operation uisng php and mysql. let say you have five (5) records to insert into table, You can choose to add 1 or more record using the below code. I fully describe on how you will successfully carry out this multiple insert statement to save time.
Configure your database using the script below
-- CREATE TABLE IF NOT EXISTS `multipleoperation` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
Html code for number of inputs to be generate
<form action="<?php $_SERVER['PHP_SELF']; ?>" method="get" name="amtForm"> <table width="1138" align="center"> <tr> <td width="200"> <p> <input type="text" placeholder="Enter positive Number" name="amt"<?php if(isset($_GET['view'])&&($_GET['id'])) { if($count <= 0) {?> readonly <?php } }?> id="full-width-input" class="round full-width-input" <?php if(isset($_GET["amt"])) { ?> value="<?php echo $_GET["amt"]; ?>" <?php } ?> /> <em>Enter Valid number, e.g 2.</em> </p> </td> </td> <td><button type="submit" name="submit" class="button round blue image-right ic-add text-upper"> Generate</button></td> </tr> </table> </form>
PHP code to generate desired number of inputs form
<?php // Get the amount to be generated if(isset($_GET['amt'])) { if($_GET['amt'] > 0) { ?> <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> <fieldset> <?php // Loop the rows and inputs according to the amount for($i=1; $i<=$_GET['amt']; $i++) { ?> <p> <label for="full-width-input"><?php echo $i; ?>. Name</label> <input type="text" name="name<?php echo $i; ?>" id="full-width-input" class="round full-width-input"/> <em>Enter Name's.</em> </p> <?php } }else { echo '<p style="font-size:17px; color:red;">Enter positive number and generate form to Upload Your Course</p> '; } ?> </td> </fieldset> <input type="hidden" name="total" value="<?php echo $i-1; ?>" /> <?php // Post the total rows count value ?> <input type="submit" name="create" class="button round blue image-right ic-add text-upper" value="Upload Data" /> </form> <?php } ?>
PHP code for insertion of multiple insert record
<?php require ('config.php'); // include database connection file ?> <?php $success=""; // Multipe insert case if(isset($_POST['create'])) { $amt = $_POST['total']; if($amt > 0) { $qry = "INSERT INTO multipleOperation (name) VALUES "; // Split the mysql_query for($i=1; $i<=$amt; $i++) { $name=($_POST["name$i"]); $qry .= "('".$name."'), "; // loop the mysql_query values to avoid more server loding time /////////////////////////////////////////////////////////////////////////////////////////////////////// $success='<div class="confirmation-box round">Operation Successful!</div>'; } $qry = substr($qry, 0, strlen($qry)-2); $insert = $db->query($qry); // Execute the mysql_query } } ?>
Complete Code
<?php require ('config.php'); // include database connection file $success=""; // Multipe insert case if(isset($_POST['create'])) { $amt = $_POST['total']; if($amt > 0) { // if greater than zero $qry = "INSERT INTO multipleOperation (name) VALUES "; // Split the mysql_query for($i=1; $i<=$amt; $i++) { // run for loop $name=($_POST["name$i"]); $qry .= "('".$name."'), "; // loop the mysql_query values to avoid more server loding time /////////////////////////////////////////////////////////////////////////////////////////////////////// $success='<div class="confirmation-box round">Operation Successful!</div>'; } $qry = substr($qry, 0, strlen($qry)-2); $insert = $db->query($qry); // Execute the mysql_query } } ?> <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <form action="<?php $_SERVER['PHP_SELF']; ?>" method="get" name="amtForm"> <table width="1138" align="center"> <tr> <td width="200"> <p> <input type="text" placeholder="Enter positive Number" name="amt"<?php if(isset($_GET['view'])&&($_GET['id'])) { if($count <= 0) {?> readonly <?php } }?> id="full-width-input" class="round full-width-input" <?php if(isset($_GET["amt"])) { ?> value="<?php echo $_GET["amt"]; ?>" <?php } ?> /> <em>Enter Valid number, e.g 2.</em> </p> </td> <td><button type="submit" name="submit" class="button round blue image-right ic-add text-upper"> Generate</button> </td> </tr> </table> </form> <?php // display success message echo $success; // Get the amount to be generated if(isset($_GET['amt'])) { if($_GET['amt'] > 0) { ?> <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post"> <fieldset> <?php // Loop the rows and inputs according to the amount for($i=1; $i<=$_GET['amt']; $i++) { ?> <p> <label for="full-width-input"><?php echo $i; ?>. Name</label> <input type="text" name="name<?php echo $i; ?>" id="full-width-input" class="round full-width-input"/> <em>Enter Name.</em> </p> <?php } }else { echo '<p style="font-size:17px; color:red;">Enter positive number and generate form to Upload Your Course</p> '; } ?> </td> </fieldset> <input type="hidden" name="total" value="<?php echo $i-1; ?>" /> <?php // Post the total rows count value ?> <input type="submit" name="create" class="button round blue image-right ic-add text-upper" value="Upload Data" /> </form> <?php } ?> </body> </html>
Are you looking to make money from your traffic with popup ads?
ReplyDeleteIf so, did you try using Clickadu?