[SOLVED] array insert

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

[SOLVED] array insert

Post by ol4pr0 »

tried something stupid like the following

Code: Select all

foreach ($nombres_array as $nombres_insert)
$query = 'INSERT INTO opertunidades (mail) VALUES ("'.$nombres_insert.'")' or die (mysql_error());
$result = mysql_query($query);
HOwever this doesnt work, well it does but only inserts the first name of the array. How do i make a insert that will insert them all. like below

id name
1 jack
2 jill
3 diddly
Last edited by ol4pr0 on Tue Aug 31, 2004 8:33 am, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

You just appear to be missing {} , without them only the $query = line is 'inside' the foreach. Also the or die ... should be part of the mysql_query not the query string.

Code: Select all

foreach ($nombres_array as $nombres_insert){
    $query = "INSERT INTO opertunidades (mail) VALUES ('".$nombres_insert."')";
    mysql_query($query) or die(mysql_error());
}
User avatar
ol4pr0
Forum Regular
Posts: 926
Joined: Thu Jan 08, 2004 11:22 am
Location: ecuador

Post by ol4pr0 »

thx worked great

yea stupid.. copy and pasted the version i was busy editing lol.
it was only inserting the 1st one.. but now they luckly did them all hehe.
Post Reply