hi frens..

i am working on a project, where, i had to add rows to table dynamically on click of button. i got to work that part, but got stuck with getting to save the data entered into db. i have not missed to add "[]" to the names in input tag. i have posted the code below, its working partially. lets say user enters 2 rows of data, $itemId and $poId are getting saved properly, but for quantity, rate and total only one row of data is getting saved, where am i going wrong, can anyone pls point out??

i have spent last 10 hours on this phew:sad:

also, is the method i am using correct, is there any other better/efficient method to do the same??

$component = $_POST['component'];
$quantity[] = $_POST['quantity'];
$rate[] = $_POST['rate'];
$total[] = $_POST['total'];
		
$i=0;
			
foreach($_POST['component'] as $item)
{
$sqlI="select * from component_item where name='".$item."'";
$resI=mysql_query($sqlI);
$rowI=mysql_fetch_array($resI);
$itemId[]=$rowI['id'];
}

foreach($_POST['quantity'] as $temp)
{
	$quantity[]=$temp;
}

foreach($_POST['rate'] as $temp)
{
	$rate[]=$temp;
}

foreach($_POST['total'] as $temp)
{
	$total[]=$temp;
}


for($i=0;$i<count($itemId);$i++)
{
	$query="insert into purchase_order_detail values('','$itemId[$i]','$quantity[$i]','$rate[$i]','$total[$i]','$poId')";
	$result=mysql_query($query);
}

thanks ppl...

Member Avatar for diafol

I think your arrays are mashed.

$quantity[] = $_POST['quantity'];
$rate[] = $_POST['rate'];
$total[] = $_POST['total'];

I don't know if you need to do this as you're doing a foreach and adding items anyway.
You can do the below OR a foreach:

$quantity = $_POST['quantity'];
$rate = $_POST['rate'];
$total = $_POST['total'];

This also looks a bit wasteful:

foreach($_POST['component'] as $item)
{
$sqlI="select * from component_item where name='".$item."'";
$resI=mysql_query($sqlI);
$rowI=mysql_fetch_array($resI);
$itemId[]=$rowI['id'];
}

You could have a more efficient SQL using the IN keyword - you can just use the one sql statement instead of a loop.

thank you ardav..that helped a lott

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.