1

I have a misunderstanding regarding some dynamic row inserts in a MySQL table.

All I want to do is based on the EAV Model:

  • I have a products table which contains products
  • A attributes table which contains attributes (name) for products (group based - related to category)
  • And a product_attribute which contains the value of the attribute

And the user must create this attributes to be associated with a product, and I'm thinking to insert <input> elements with jQuery's append.

But how to track inserts and how to append them to have a specific name : <input name=''>.

The result should do the following:

<form action='attribute_insert.php' method='post'>
<input name='field1' />
<input name='field2' />
<input name='field3' />
....
<input name='field20' />
</form>

and after processing in attribute_insert.php to insert like this :

('1', '12', 'manufacturer');
('2', '12', 'freq');
('3', '12', 'memory');
('4', '12', 'ram');
....
('20', '12', 'blablabla');

How can I do this ... using foreach to scan the $_POST[] or there is a clever method?

1 Answer 1

3

Why not name them field[], then you don't have to worry about the number. In PHP you can just do a for loop over $_POST['field'], since it will return an array of the fields ;)

Sign up to request clarification or add additional context in comments.

4 Comments

I was just typing up the same thing :P +1 to you for thinking the same as me.
I don't get it ? how ? the <input type='text' name='???'/> is appended via jquery ...
Terw ? please explain ... if I have <input name='field'/> and <input name='field'/> the $_POST[] will interpret as an array ... like $_POST[field][0] and $_POST[field][1]? Thank you
Hi, sorry for late reply. But you have to use <input name="field[]"> with the []. Then the browser should send all of them and PHP will treat them as an array. You can also use field[1] if the number is important. If it's just random, don't include it ;)

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.