PHP implementation of the two methods of updating intermediate table data, php two ways _php tutorial

Source: Internet
Author: User
Tags sql server example sqlite database

PHP implements two methods of updating intermediate relational table data, PHP two methods


This paper shows the two methods of updating the Data of Intermediate Association table in PHP. Share it for everyone's reference. Here's how:

First, the Middle Association table: Here the intermediate table is only the primary key of table 1 and the primary key of table 2, that is, many-to-many forms.
Perform data additions and deletions as internal methods of the framework, not part of the thinking section.

Method One: Delete all old data first, then add new data

$res = $this->classes->classedit ($id, $data);  Modify the Main table data if ($res) {//delete the associated 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 succeeded! ');} else{$this->show_tips (' Operation failed! ');}

The disadvantage of using this method: large-scale deletion of data this block is not safe, there is a certain degree of security risks.

Method Two: Only add the necessary, only delete the

Old data detected in the library: $arr _old (processed one-dimensional array)//New data submitted: $arr _new (the resulting one-dimensional array) $intersect = Array_intersect ($arr _old, $arr _new);    Intersection (the part that needs to be preserved, without processing) $result _del = Array_diff ($arr _old, $intersect);    $result_add = Array_diff ($arr _new, $intersect) that need to be removed from the old data; New data needs to be added//Add 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); Perform add  }}//Delete the data that needs to be purged 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 succeeded! ');} else{$this->show_tips (' Operation failed! ');}

The method features: targeted to add data and delete data, more than the first method, high security

I hope this article is helpful to everyone's PHP programming.


How does PHP correlate two or more data tables?

Select u.qq,u.name,p.* from posts as p left joins users as U on P.name=u.name

Teach SQL statements how to implement associated table field updates

Update multiple table Updates (RPM) (2008-05-12 15:29:04)
Reprint Tags: update multiple table updates SQL classification: PHP network programming
In development, the database is swapped back and forth, and some key grammars are different, which is a headache for developers. This article summarizes the use of UPDATE statements in SQL Server,oracle,mysql three databases when updating multiple tables. I tried the SQLite database, Are not successful, I do not know whether to support multi-table updates or what.

In this example, we will use the Gqdltks,bztks field data in table GDQLPJ to update the data for the same field name in Landleveldata, if geo_ in Landleveldata The Code field value is updated when the value of the LXQDM field in GDQLPJ is equal.

SQL Server syntax: UPDATE {table_name with (< table_hint_limited > [... n]) | view_name | rowset_function_limited} SE T {column_name = {expression | DEFAULT | NULL} | @variable = Expression | @variable = column = expression} [,... N] {{[from {< Table_source >} [,... n]] [WHERE < Search_conditio n >]} | [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])

Oracel Example: Update Landleveldata a set (A.gqdltks, A.bztks) = (select B.gqdltks, b.bztks from GDQLPJ b where a.geo_code=b.lx Qdm

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

http://www.bkjia.com/PHPjc/872373.html www.bkjia.com true http://www.bkjia.com/PHPjc/872373.html techarticle PHP Implementation of the two methods of updating the data of the intermediate tables, PHP Two methods This article shows a sample of the PHP implementation of updating the data of the Intermediate association table two methods. Share to everyone for your reference ...

  • 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.