insert multiple rows into oracle database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gammaman
Forum Commoner
Posts: 45
Joined: Tue Feb 12, 2008 9:22 am

insert multiple rows into oracle database.

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: insert multiple rows into oracle database.

Post 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.
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: insert multiple rows into oracle database.

Post 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.
Post Reply