foreach question

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
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

foreach question

Post by a94060 »

hi,i have this code over here

Code: Select all

$q = array(
"1" => "INSERT INTO reg_users (user_id,username,password,registered,email) VALUES ('', '$name' , '$password' , '$time' , '$email')",
 2 => "INSERT INTO profile (username, Name, Location, Gender, Birth_date, registered, profile_pic) VALUES('$name' , 'Name', 'Location', 'Gender', 'Birth Date', $time, 'Picture URL.')",
 3 => "INSERT INTO style (main_HEADER_text, main_bgcolor, main_HEADER_fontcolor, main_fontstyle, main_fontface, border_color, border_style, border_size, mod_titlebarBG, mod_titlefontcolor, mod_titlefontface, username) VALUES
('$name', 'White', 'Black', 'Bold', 'Verdana', 'Black', 'Solid', '1', 'White', 'Black', 'Verdana', )")


the thing i want to do is foreach of the of the array vaules,i want the database to do the query.

how can i acheave this?
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Code: Select all

foreach($q as $query) {
    mysql_query($query);
}
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

i had put that in my zend program and it kept saying something like unexpected thing at end of line. is the array correct? like is everyhtin gin there correct?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

I've never gotten an "unexpected thing at end of line" error, are you sure it said "undexpected thing"?
User avatar
a94060
Forum Regular
Posts: 543
Joined: Fri Feb 10, 2006 4:53 pm

Post by a94060 »

it didnt say thing,i for got what it said there though. :lol:
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

well...

it may have said 'unexpected thing'. You have issues in your sql strings... for example

Your first array key is string and rest of them are ints. Not sure if that woud cause problem but I'd keep it consistent.

Your first sql statement

Code: Select all

"INSERT INTO reg_users (user_id,username,password,registered,email) VALUES ('', '$name' , '$password' , '$time' , '$email')"
You are inserting empty user_id... That will probably crash if your user_id is primary key

Last sql statement

Code: Select all

"INSERT INTO style (main_HEADER_text, main_bgcolor, main_HEADER_fontcolor, main_fontstyle, main_fontface, border_color, border_style, border_size, mod_titlebarBG, mod_titlefontcolor, mod_titlefontface, username) VALUES
('$name', 'White', 'Black', 'Bold', 'Verdana', 'Black', 'Solid', '1', 'White', 'Black', 'Verdana', )"
You have 12 rows in your insert into and only 11 in values.
Right after 'Verdana' you have a comma which will cause problems. You need one more field to complete this.

I'd first make sure sql statements execute correctly then continue. Errors are there to help you debug not to be ignored. They'll bite you in the A55

Post Reply