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?

palacsint
29k11 gold badges85 silver badges113 bronze badges
asked Dec 17, 2011 at 12:07

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')
answered Dec 17, 2011 at 12:10
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.