timmah1 Posted March 14, 2012 Share Posted March 14, 2012 What I' trying to do is take my array $stores = array("name"=>"$MktName","address"=>"$address"); And for each each value, insert it into my db foreach($stores as $k => $v){ mysql_query("INSERT INTO stores (name, address) VALUES('$k', '$v' ) ") or die(mysql_error()); } Now, I'm just learning more about arrays, and this is obviously wrong How do I go about getting the value of "name"? It pulls the value for "address". Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/ Share on other sites More sharing options...
marcbraulio Posted March 14, 2012 Share Posted March 14, 2012 I am not an expert and the way I learned CRUD was by using PDO prepared statements, so I am not 100% familiar with the original mysql format. But as far as I know, to achieve what you want, you would have to do the following: $stores = array("$MktName", "$address"); mysql_query("INSERT INTO stores (name, address) VALUES("implode(',', $stores)") or die(mysql_error()); Because with your current code you are producing this: mysql_query("INSERT INTO stores (name, address) VALUES('address', '$address') or die(mysql_error()); mysql_query("INSERT INTO stores (name, address) VALUES('name', '$MktName') or die(mysql_error()); And that makes no sense since the Columns have to match the Values. Instance: $stores = array("$MktName", "$address"); mysql_query("INSERT INTO stores (name, address) VALUES("$MktName", "$address") or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327103 Share on other sites More sharing options...
timmah1 Posted March 14, 2012 Author Share Posted March 14, 2012 that makes sense, thank you I'm not getting an error though You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327105 Share on other sites More sharing options...
Pikachu2000 Posted March 14, 2012 Share Posted March 14, 2012 The query string should end up in this format: INSERT INTO table (field1, field2) VALUES ('value1a', 'value2a'), ('value1b', 'value2b'), ('value1c', 'value2c') That way you can execute one query instead of running a query in a loop, which is usually a bad idea. Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327107 Share on other sites More sharing options...
marcbraulio Posted March 14, 2012 Share Posted March 14, 2012 that makes sense, thank you I'm not getting an error though You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Well, what do you have on line 1? Check for any missing/misplaced semi-colons, parenthesis, or quotes. Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327109 Share on other sites More sharing options...
Pikachu2000 Posted March 14, 2012 Share Posted March 14, 2012 That's a MySQL error, so it refers to line 1 of the query string, not the php script. The query string should be echoed and inspected for problems. Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327112 Share on other sites More sharing options...
marcbraulio Posted March 14, 2012 Share Posted March 14, 2012 That's a MySQL error, so it refers to line 1 of the query string, not the php script. The query string should be echoed and inspected for problems. And this is when I exit the room in shame... lol Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327113 Share on other sites More sharing options...
timmah1 Posted March 14, 2012 Author Share Posted March 14, 2012 Thank you everybody for your help. This was a 1 shot thing doing this, I just needed to put those 14,000 entries into a db instead of entering them one by one Like always, I over-thought the process. Anyhow, I learned today, so it was good. Thank you for your help Quote Link to comment https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327116 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.