[PHP] Two ideas for updating intermediate joined table data: php ideas

Source: Internet
Author: User
Tags php database

[PHP] Two ideas for updating intermediate joined table data: php ideas

/* Configure /*---------------------------------------------------------------------------------------------------------

@ Blackeye poet <www. chenwei. ws>

Optional ---------------------------------------------------------------------------------------------------------*/

Intermediate join table: the intermediate table only stores the primary key of table 1 and the primary key of table 2, that is, multiple to multiple forms.

(Adding and deleting data is not part of the idea)

 

/*
| -----------------------------------------------------------
| Method 1: Delete all old data and then add new data
| @ Chenwei <www. chenwei. ws> | -----------------------------------------------------------
*/$ 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 ){
// Cyclically assemble conditions to 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! ');}

// Disadvantage: it is unsafe to delete data in large batches, which poses a certain level of security risks.

 

 

/*
| ---------------------------------------------------------
| Method 2: add only what is needed and delete only what is to be deleted
| @ Chenwei <www. chenwei. ws> | ---------------------------------------------------------
* /// 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 = $ this-> 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! ');
}


// Features: Targeted addition and deletion of data, higher security than the first method

 

If there are any errors or better solutions, please leave a message.


Association between two tables A and B in the MYSQL + PHP Database

Select 'A'. *, 'B'. 'mentor name' WHERE 'A'. 'mentor id' = 'B'. 'mentor id'
The instructor ID in Table A is an index.
The tutor ID in Table B is the primary key.

This is not clear .. Helpless
You write the field name in your database, instead of text.
What else do I need to retrieve?
Write the SQL directly.

If ($ condition = ""){
$ Condition = "WHERE sid LIKE \" % ". $ id." % \"";
} Else {
$ Condition. = "AND sid LIKE \" % ". $ id." % \"";
}
Change
Condition = "AND sid LIKE \" % ". $ id." % \"";

Then SQL
$ SQL = 'select 'data '. *, 'teacher '. 'tname' FROM 'data', 'teacher' WHERE 'data '. 'tid' = 'teacher '. 'tid ''. $ condition
That's it.

In php, I used a CI framework to develop a program. I have two tables connected. Can I delete the data associated with two tables at the same time? How do I write the deleted code?

Delete from A where. ID in (select B. ID from B where B. sum> 100) This is just an example of table connection deletion and table connection query select * from a left join B on. id = B. id where. id = 1 update a, B set. id = 12 wehre B. id = 2 not tested

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.