Jump to content

array and foreach


timmah1

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/258878-array-and-foreach/
Share on other sites

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()); 

 

Link to comment
https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327103
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327107
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327109
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/258878-array-and-foreach/#findComment-1327116
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.