This Code is for inserting Units for certain specifications. for example, if specification is length, then units are centimeter, meter and millimeter.
When I try to read all units in single text field with commas and in PHP tried to explode the units with comma. But when I submit the form only the first unit is saved to DB.
This is my database structure:
CREATE TABLE IF NOT EXISTS `tbl_unit` (
`unit_id` varchar(5) NOT NULL,
`unit_name` varchar(50) NOT NULL,
`specification_id` int(11) NOT NULL,
PRIMARY KEY (`unit_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This is my code:
<?php
include("../config.php");
$uid=$_POST['unitid'];
$unit=$_POST['unitname'];
$spec=$_POST['specification'];
$arr1 = explode(',',$unit);
$size=count($arr1);
for($i=0;$i<$size;$i++)
{
mysql_query("insert into tbl_unit values('".$uid."','$arr1[$i]','".$specification."')");
}
header('Location:addunit.php');
?>
What is confusing me is that when I try to insert $arr1[0]
, $arr1[1]
or $arr1[2]
separately the values are being saved. I think the for loop is executing only once. what is the problem with for loop?