#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Canada
    Posts
    1
    Rep Power
    0

    Inserting elements of multiple arrays in database


    Hello everyone.

    I am stuck when trying to pass the contents of multiple arrays to an INSERT statement. Here is my code:

    foreach ($_POST['samp'] as $samp)
    {
    $query = "INSERT INTO sample (soid, reqno, wno, sname, primer, readlen, comment, statid) VALUES (nextval('IncSample'), '{$_POST['reqNo
    ']}', 0, '$samp', '{$_POST['primer']}', '{$_POST['rlbutt{$i}']}', '{$_POST['comment']}', 1)";


    pg_query($db, $query);
    $i++;
    }

    Basically, my script takes global data passed from the previous page and inserts into a PostgreSQL db. I have no problem with soid, reqNo, wno or statid. The problem is with the sname, primer, rlbutt{$i} ('read length' passed from radio button) readlen, and comment fields. Using the above 'foreach' loop results in the proper data being inserted for $_POST['samp'], but not the others. I know the data is getting to this page / script as echoing print_r($_POST) correctly displays all the data elements and associated arrays.

    The result in my database looks like this:

    | soid | reqno | wno | sname | primer | readlen | comment | statid |

    | 101 | 32 | 0 | s1 | Array | | Array | 1 |


    The first 3 (and last) fields are fine. 'sname' because of the condition of the foreach loop works fine, however, primer and comment display 'Array' and readlen is left balnk.

    Any suggestions would be much appreciated.

    Thanks in advance,
    Jordan
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    1
    Rep Power
    0
    Originally Posted by jkoll
    foreach ($_POST['samp'] as $samp)
    {
    $query = "INSERT INTO sample (soid, reqno, wno, sname, primer, readlen, comment, statid) VALUES (nextval('IncSample'), '{$_POST['reqNo
    ']}', 0, '$samp', '{$_POST['primer']}', '{$_POST['rlbutt{$i}']}', '{$_POST['comment']}', 1)";


    pg_query($db, $query);
    $i++;
    }


    Jordan,

    I ran into a similar problem while first trying to insert arrays. Have you tried performing the query inside the loop rather than assigning a variable to it?

    Try this:

    foreach ($_POST['samp'] as $samp)
    {
    mysql_query("INSERT INTO sample (soid, reqno, wno, sname, primer, readlen, comment, statid) VALUES (nextval('IncSample'), '{$_POST['reqNo
    ']}', 0, '$samp', '{$_POST['primer']}', '{$_POST['rlbutt{$i}']}', '{$_POST['comment']}', 1)");


    pg_query($db, $query);
    $i++;
    }

    Let me know if that helps...


IMN logo majestic logo threadwatch logo seochat tools logo