Page 1 of 1

insert multiple rows into oracle database.

Posted: Tue Apr 06, 2010 4:28 pm
by gammaman
I need help inserting multiple rows into an Oracle database. I get some errors. I think it is because I need to be using a loop to do the insert but I do not know how. I could also possibly be doing the binding wrong. I start out with a form and then POST to a PHP page and go from there.

Here is the form in short notation

Code: Select all

form.php
for($i=0;$i<=10;$i++)
{
<input fname="first[]/>
<input lname="last[]"/>
<input age ="age[]"/>
}

insert.php

Code: Select all

$fname = $_POST['first'];
$lname = $_POST['last'];
$age = $_POST['age'];
** NOTE:  These are now arrays.

***** I think somewhere here I need to do a loop for the insert
 
$result = oci_parse($conn, 'INSERT INTO TABLE (first,last,age) VALUES (:first,:last,:age)');

**** I get an error on the binds saying I cannot have an array to string conversion.

oci_bind_by_name($result, ':first',$first);
oci_bind_by_name($result, ':last',$last);
oci_bind_by_name($result, ':age',$age);
Since all of the fields are not null and I have ten rows in the form according to the for loop, I would like to somehow check to see which rows are blank. Meaning that they were not filled out and get rid of them. Otherwise there would be an error on insert becasue the fields are not null.

Re: insert multiple rows into oracle database.

Posted: Wed Apr 07, 2010 1:15 am
by JakeJ
I don't know Oracle syntax but I can help with a few things.

1. Echo out your data to make sure you actually have data in your variables.
2. Make sure your insert query works directly with an oracle management tool rather than through PHP code to make sure it's a valid query.
3. Make sure your data types match your fields in Oracle.
3. When you think you have the syntax down, try the insert and if it doesn't work, go on field at a time to see where it bombs out.

Re: insert multiple rows into oracle database.

Posted: Wed Apr 07, 2010 5:03 am
by roders
You are right about using a loop to insert.
I would personally use a multidimensional array to do such a fing.
http://www.php-learn-it.com/php_arrays.html
Using multidimensional array would allow you easily to fix this.
try to see if you can understand a bit, try to code it. if there's any prob post back and we will help.