Hello. I have a JavaScript in the form that dynamically adds an input field to the form. The newly added field has the following attributes:

 <input value="<?=$prp['name'];?>" name="prpunkt[]<?=$ipp?>" type="text" /> 

where $ prp ['name'] is the text from the base. $ ipp - $ipp++ ;

The task is this, you need to make editing existing fields in the database, and adding new ones.

And everything would be fine, but. Each field is attached to a specific article. Table structure with fields:

id - AUTO_INCREMENT

trip INT - id of the article to which we bind the field

name varchar - field values ​​(text in the field)

An indefinite number of fields may be attached to the article.

When editing an article, it is possible to edit already existing add. fields and create new ones.

Already existing fields, when editing the article, I display the cycle:

 while ($prp = mysql_fetch_assoc($option_prp)) { $ipp++; //echo $ipp; ?> <div><input class="stationinppp" value="<?=$prp['name'];?>" name="prpunkt[]<?=$ipp?>" type="text" /></div> <? } 

That is, it turns out that if this article is added, for example, 3 additional. fields, the markup is as follows:

 <input value="<?=$prp['name'];?>" name="prpunkt[]1" type="text" /> <input value="<?=$prp['name'];?>" name="prpunkt[]2" type="text" /> <input value="<?=$prp['name'];?>" name="prpunkt[]3" type="text" /> 

And the newly added field has markup:

 <input value="<?=$prp['name'];?>" name="prpunkt[]4" type="text" /> <input value="<?=$prp['name'];?>" name="prpunkt[]5" type="text" /> 

The value of name - name="prpunkt[]+1"

How can I determine on the server, which field has been changed, and which field has been added? This is for me to perform the corresponding requests to the database, to the new field INSERT INTO - write a new line to the base / to the modified field UPDATE - update the record.

The data from the fields on the server is caught as follows:

 foreach ($_POST['prpunkt'] as $k => $f) { echo $_POST['prpunkt'][$k]; } 
  • Immediately the question .. why not use the database INSERT ON DUPLICATE KEY UPDATE ? - Alexey Shimansky
  • @ Alexey Shimansky can any example with this? - iKey
  • uh ... dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html ? or ru.stackoverflow.com/search?q=INSERT+ON+DUPLICATE+KEY+UPDATE choose ... the point is to install a unique key in the database on the field (s) and when you request the form INSERT INTO ON DUPLICATE KEY - new records were added, and old ones (if they coincide with those in the request) were updated - Alexey Shimansky
  • @ Alexey Shimansky mysql_query ("INSERT INTO prom_item ( trip , name ) values ​​('$ nw_id', '$ namepp') ON DUPLICATE KEY UPDATE name = '$ namepp'"); The name field is unique. but when you edit a field, a new one is added. What am I missing? - iKey
  • Vsmysle "added new"? If you have added a uniqueness to the name, then another entry with the same name cannot appear there. - Arnial

0