PHP implements two methods to update the data in the intermediate joined table: php. PHP implements two methods for updating intermediate joined table data. php provides two methods. This document provides an example to demonstrate two methods for updating intermediate joined table data in PHP. I would like to share with you the following two methods for updating the data in the intermediate joined table in PHP:
This document provides an example of two methods for updating the data in the intermediate join table in PHP. Share it with you for your reference. The specific method is as follows:
First, the intermediate join table: the intermediate table only stores the primary key of Table 1 and the primary key of table 2, that is, the multi-to-many form.
Adding and deleting data is not part of the idea.
Method 1: delete all old data and then add new data.
$ Res = $ this-> classes-> classEdit ($ id, $ data); // modify the primary table data if ($ res) {// delete joined table data first $ bool = $ this-> lesson_classes-> lessonClassesDel ($ id); if ($ bool) {// cyclic assembly condition, add data foreach ($ lesson_ids as $ val) {$ arr = array ('class _ id' => $ id, 'lesson _ id' => $ val ); // Data $ res = $ this-> lesson_classes-> lessonClassesAdd ($ arr); // execute add} $ this-> show_tips ('Operation successful! ');} Else {$ this-> show_tips (' Operation failed! ');}
Disadvantages of using this method: it is not safe to delete data in large batches, and there is a certain degree of security risks.
Method 2: add only what is needed and delete only what is to be deleted
// The old data found in the database: $ arr_old (processed one-dimensional array) // The new data submitted: $ arr_new (the obtained one-dimensional array) $ intersect = array_intersect ($ arr_old, $ arr_new); // intersection (the part to be retained, not to be processed) $ result_del = array_diff ($ arr_old, $ intersect ); // $ result_add = array_diff ($ arr_new, $ intersect) to be deleted from the old data ); // add new data to the new data if ($ result_add & is_array ($ result_add) {foreach ($ result_add as $ val) {$ data_add = array ('class _ id' => $ id, 'lesson _ id' => $ val); // Data $ bool_add = $ th Is-> lesson_classes-> lessonClassesAdd ($ data_add); // execute add} // delete the data to be cleared if ($ result_del & is_array ($ result_del )) {foreach ($ result_del as $ val) {$ bool_del = $ this-> lesson_classes-> lessonClassesDel ($ id, $ val ); // execute delete} if ($ bool_add & $ bool_del) {$ this-> show_tips ('Operation successful! ');} Else {$ this-> show_tips (' Operation failed! ');}
This method features: targeted addition and deletion of data, which is more secure than the first method.
I hope this article will help you with PHP programming.
How does php associate two or more data tables?
Select u. qq, u. name, p. * from posts as p left join users as u on p. name = u. name
Teaches SQL statements how to update joined table fields
UPDATE multi-table UPDATE (to) (15:29:04)
Update multi-table update SQL classification: php network programming
During development, the database is switched back and forth, and some key syntaxes are different. this is a headache for developers. this article summarizes the usage of the Update statement in SQL Server, Oracle, and MySQL databases when updating multiple tables. I also tried the SQLite database, but I didn't know whether multi-table update is supported or not.
In this example: Use the gqdltks and bztks fields in the gdqlpj table to update the data of the same field name in landleveldata, the condition is that the GEO_Code field value in landleveldata is updated when it is equal to the lxqdm field value in gdqlpj.
SQL Server syntax: UPDATE {table_name WITH (<table_hint_limited> [... n]) | view_name | rowset_function_limited} SET {column_name = {expression | DEFAULT | NULL} | @ variable = expression | @ variable = column = expression }[,... n] {[FROM {<table_source> }[,... n] [WHERE <search_condition>]} | [where current of {[GLOBAL] cursor_name} | cursor_variable_name}]} [OPTION (<query_hint> [,... n])]
SQL Server example: update a set a. gqdltks = B. gqdltks, a. bztks = B. bztks from landleveldata a, gdqlpj B where a. GEO_Code = B. lxqdm
Oracle syntax: UPDATE updatedtable SET (col_name1 [, col_name2...]) = (SELECT col_name1, [, col_name2...] FROM srctable [WHERE where_definition])
Example of cancel: update landleveldata a set (a. gqdltks, a. bztks) = (select B. gqdltks, B. bztks from gdqlpj B where a. GEO_Code = B. lxqdm)
MySQL syntax: UPDATE table_references SET col_name1 = expr1 [, col_name2 = expr2...] [WHERE where_definition]
MySQL example: update landleveldata a, gdqlpj B set a. gqdltks = B. gqdltks, a. bztks = B. bztks where a. GEO_Code = B. lxqdm
The following example shows two methods for updating data in the intermediate joined table in PHP. Share it with you for your reference...