Sunday, June 3, 2012

Insert Multiple Records to Mysql table



Insert Multiple Records to Mysql table

One of  the previous post,I have discussed the way of inserting single record to mysql table.This time I am going to display you the way of adding multiple records in one click.You have the database,my_database now, Next create the following table in my_database.

CREATE TABLE IF NOT EXISTS `item` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `level` varchar(20) NOT NULL,
  `annual_tar` int(5) NOT NULL,
  `progress` int(5) NOT NULL,
  `issues` int(5) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Then create the input form as bellow


Item Production

Product Name Annual Target Production Issues


Codes for above input table is bellow.

<center><table bgcolor="#0b2d04">
<form action="product_ent.php" method="post">
<tbody>
<tr bgcolor="#dbaa7a"><td align="center" colspan="6"><h2>


Item Production</h2>
</td></tr>
<tr>
<th><span style="color: white;">Product Name</span></th>
<th><span style="color: white;">Annual Target</span></th>
<th><span style="color: white;">Production</span></th>
<th><span style="color: white;">Issues</span></th>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level" readonly="readonly" type="text" value="Item1" /></td>
<td><input name="annual_tar" type="text" /></td>
<td><input name="progress" type="text" /></td>
<td><input name="issues" type="text" /></td>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level2" readonly="readonly" type="text" value="Item2" /></td>
<td><input name="annual_tar2" type="text" /></td>
<td><input name="progress2" type="text" /></td>
<td><input name="issues2" type="text" /></td>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level3" readonly="readonly" type="text" value="Item3" /></td>
<td><input name="annual_tar3" type="text" /></td>
<td><input name="progress3" type="text" /></td>
<td><input name="issues3" type="text" /></td>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level4" readonly="readonly" type="text" value="Item4" /></td>
<td><input name="annual_tar4" type="text" /></td>
<td><input name="progress4" type="text" /></td>
<td><input name="issues4" type="text" /></td>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level5" readonly="readonly" type="text" value="Item5" /></td>
<td><input name="annual_tar5" type="text" /></td>
<td><input name="progress5" type="text" /></td>
<td><input name="issues5" type="text" /></td>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level6" readonly="readonly" type="text" value="Item6" /></td>
<td><input name="annual_tar6" type="text" /></td>
<td><input name="progress6" type="text" /></td>
<td><input name="issues6" type="text" /></td>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level7" readonly="readonly" type="text" value="Item7" /></td>
<td><input name="annual_tar7" type="text" /></td>
<td><input name="progress7" type="text" /></td>
<td><input name="issues7" type="text" /></td>
</tr>
<tr bgcolor="#e6f293">
<td><input name="level8" readonly="readonly" type="text" value="Item8" /></td>
<td><input name="annual_tar8" type="text" /></td>
<td><input name="progress8" type="text" /></td>
<td><input name="issues8" type="text" /></td>
</tr>
<tr bgcolor="#dbaa7a"><td align="right" colspan="6"><input type="submit" value="Enter" /></td></tr>
</tbody></table>
</center></form>

product_ent.php Which carries above input to mysql table and stores is bellow

 <?php
$username="root";
$password="";
$database="my_database";


$level=$_POST['level'];
$level2=$_POST['level2'];
$level3=$_POST['level3'];
$level4=$_POST['level4'];
$level5=$_POST['level5'];
$level6=$_POST['level6'];
$level7=$_POST['level7'];
$level8=$_POST['level8'];


$annual_tar=$_POST['annual_tar'];
$annual_tar2=$_POST['annual_tar2'];
$annual_tar3=$_POST['annual_tar3'];
$annual_tar4=$_POST['annual_tar4'];
$annual_tar5=$_POST['annual_tar5'];
$annual_tar6=$_POST['annual_tar6'];
$annual_tar7=$_POST['annual_tar7'];
$annual_tar8=$_POST['annual_tar8'];



$progress=$_POST['progress'];
$progress2=$_POST['progress2'];
$progress3=$_POST['progress3'];
$progress4=$_POST['progress4'];
$progress5=$_POST['progress5'];
$progress6=$_POST['progress6'];
$progress7=$_POST['progress7'];
$progress8=$_POST['progress8'];


$issues=$_POST['issues'];
$issues2=$_POST['issues2'];
$issues3=$_POST['issues3'];
$issues4=$_POST['issues4'];
$issues5=$_POST['issues5'];
$issues6=$_POST['issues6'];
$issues7=$_POST['issues7'];
$issues8=$_POST['issues8'];


mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query ="INSERT INTO item(id,level,annual_tar,progress,issues) VALUES
('','$level','$annual_tar','$progress','$issues')
,('','$level2','$annual_tar2','$progress2','$issues2')
,('','$level3','$annual_tar3','$progress3','$issues3')
,('','$level4','$annual_tar4','$progress4','$issues4'),
('','$level5','$annual_tar5','$progress5','$issues5')
,('','$level6','$annual_tar6','$progress6','$issues6'),
('','$level7','$annual_tar7','$progress7','$issues7')
,('','$level8','$annual_tar8','$progress8','$issues8')";
mysql_query($query);
mysql_close();
echo
"<font color='red'>You have sucessfully entered data </font>"
?>

Viewing Your data create the file as bellow.

<html>
<body>
<center><table bgcolor=#dbaa7a>
<tr><td colspan=7 align='center'><h2>Item Production</h2></td></tr>
<tr bgcolor=#ffffff>
<th>Product Name</th>
<th>Annual target</th>
<th>Production </th>
<th>Issue</th>
</tr>
<?php
$username="root";
$password="";
$database="my_database";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "(SELECT * FROM item )";
$result = mysql_query($query);
mysql_close();

while ($row = mysql_fetch_assoc($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><?php echo $row['level']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['annual_tar']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['progress']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $row['issues']; ?></td>
</tr>
<?php
}
?>
</center>
</table>
</body>
</html>

13 comments: