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');
| SitePoint Sponsor |


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');
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).';';
Edit: Bah! Beaten to it!PHP Code:<?php
$aNumb = array(1, 2, 3);
$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])
)
);
}
?>
@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.


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!!??


okk i removed mysql_real_escape_string()
ad it works fine now thanksssssssssssssssssssssssssssssssssssssssssssss
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?
link identifier is not specified
my mobile portal
ghiris.ro


last question please
i did the insert successfully but What about the Update statement?????
Bookmarks