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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.