1

Let's say that I send a JS array to a php page using an http post:

var tags = ['foo', 'bar'];

My goal is to do a new INSERT in my db table for each tag (because I suppose I can't insert multiple rows with one query)? How do I loop through the array and insert the values in to the table using mysql_query?

1 Answer 1

1

Send the post parameters in form tags[]=foo&tags[]=bar. PHP should receive them as a $_REQUEST['tags'] array. Then iterate as normal in PHP.

Alternately, use JSON.stringify(thing) on clientside to get a string representation you can send as a single parameter, then restore it with json_decode(param) on PHP side - and again you get a PHP array.

You can iterate a PHP array using the foreach ($array as $element) { ... } construct.

Also, you can insert multiple rows with one INSERT:

INSERT INTO mytable (col1, col2)
VALUES
  ('foo', 'bar'),
  ('baz', 'moo')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.