Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert multiple rows with one sql statement 1

Status
Not open for further replies.

sd0t1

IS-IT--Management
Mar 14, 2007
131
US
Hi guys, I am creating dozens of rows in a form from a database. Each row has about 10 fields. I need to be able to update all or some of the rows then INSERT them back into the database with one submit button.

I've done some research and it looks like I need to use the FOREACH loop, but I can't seem to get my head around how to write the code for inserting 10 or so fields and 30 or so rows.

Do I need to create a FOREACH for every field:

$arrOid = $_POST['oid']; foreach($arrOid as $key=>$value)
$arrPid = $_POST['pid']; foreach($arrPid as $key=>$value)
$arrCase = $_POST['case_no']; foreach($arrCase as $key=>$value)
and so on...

My fields are as follows:
$oid = $_POST['oid'];
$pid = $_POST['pid'];
$poid = $_POST['poid'];
$cid = $_POST['cid'];
$county_id = $_POST['county_id'];
$day = $_POST['day'];
$time = $_POST['time'];
$case_no = $_POST['case_no'];
$ssn = $_POST['ssn'];

Can someone please create an example for me, I would be much appreciative.

 
you should name all of your input fields (in html) something like
Code:
name="oid[]"

then you would code like this

Code:
foreach ($_POST['oid'] as $key=>$val){
 $oid = $_POST['oid'][$key]; 
$pid = $_POST['pid'][$key]; 
$poid = $_POST['poid'][$key];
$cid = $_POST['cid'][$key];
$county_id = $_POST['county_id'][$key];
$day = $_POST['day'][$key];
$time = $_POST['time'][$key];
$case_no = $_POST['case_no'][$key];
$ssn = $_POST['ssn'][$key];
 $sql = "Insert into database table (fields....) values ('values...')";
 mysql_query($sql);
}

don't forget to cleanse each data item before using it in the query.
 
Jpadie, I've been busy on another problem and didn't get a chance to say thanks for this answer.
It was a great help.

Once again, you've saved the day for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top