SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Merge 3 arrays to insert in one row in db

    i have those three arrays for example
    $num=array(1,2,3);
    $name=array('one','two','three');
    $letter=array('m','k','n');

    what i want is to loop through all of them and insert them like that:

    insert into mydb (field1,field2.field3) values(1,'one','m');

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,141
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    PHP Code:
    $num=array(1,2,3);
    $name=array('one','two','three');
    $letter=array('m','k','n');

    $inserts = array();
    for(
    $i=0;$i<3;$i++) {
        
    $inserts[] = '('.$num[$i].',\''.mysql_real_escape_string($name[$i]).',\''.mysql_real_escape_string($letter[$i]).'\')';
    }

    echo 
    'insert into mydb (field1,field2.field3) values '.implode(',',$inserts).';'

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $aNumb 
    = array(123);
    $aName = array('one''two''three');
    $aLett = array('m''k''n');

    for(
    $iIndex 0$i 3$iIndex++)
    {
        
    mysql_query(
            
    sprintf(
                
    'INSERT INTO table (field, field, field)VALUES(%s, %s, %s)',
                
    $aNumb[$iIndex],
                
    mysql_real_escape_string($aName[$iIndex]),
                
    mysql_real_escape_string($aLett[$iIndex])
            )
        );
    }
    ?>
    Edit: Bah! Beaten to it!
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ohh sorry
    the first code gave me this errors

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'ODBC'@'localhost' (using password: NO) in
    insert into mydb (field1,field2.field3) values (1,',''),(2,',''),(3,','');

    and the other code hanged my browser
    any other solution!!??

  5. #5
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okk i removed mysql_real_escape_string()
    ad it works fine now thanksssssssssssssssssssssssssssssssssssssssssssss

  6. #6
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ausge, it's prety strange you got that error from using real_escape function. Are you sure it wasn't because of not using a login password when connecting to the server?
    Free automatic directory submission
    Relieve stress and read more about stress relief.

  7. #7
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    link identifier is not specified
    my mobile portal
    ghiris.ro

  8. #8
    SitePoint Addict
    Join Date
    Nov 2007
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    last question please
    i did the insert successfully but What about the Update statement?????


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •