Table of Contents
PHP implements two methods of updating intermediate association table data, PHP two methods
How to associate two or more data tables in PHP?
Teach sql statement how to update related table fields
Home Backend Development PHP Tutorial PHP implements two methods to update intermediate association table data, two methods in PHP_PHP tutorial

PHP implements two methods to update intermediate association table data, two methods in PHP_PHP tutorial

Jul 13, 2016 am 10:19 AM
php sql statement association surface

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

This article shows two methods of updating intermediate related table data in PHP in the form of examples. Share it with everyone for your reference. The specific method is as follows:

First, the intermediate association table: The intermediate table here only stores the primary key of table 1 and the primary key of table 2, that is, a many-to-many form.
Executing data addition and deletion are internal methods of the framework and do not belong to the idea part.

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

$res = $this->classes->classEdit($id, $data);  //修改主表数据
if($res)
{
  //先删除关联表数据
  $bool = $this->lesson_classes->lessonClassesDel($id);
  if($bool)
  {
    //循环组装条件,添加数据
    foreach($lesson_ids as $val)
    {
      $arr = array('class_id'=>$id, 'lesson_id'=>$val);    //数据
      $res = $this->lesson_classes->lessonClassesAdd($arr);  //执行添加
    }
  }
  $this->show_tips('操作成功!');
}
else
{
  $this->show_tips('操作失败!');
}

Copy after login

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 you need and delete only what you want

//库中查出的旧数据:$arr_old (处理过的一维数组)
//提交过来的新数据:$arr_new (得到的一维数组)

$intersect = array_intersect($arr_old, $arr_new);   //交集(需要保留的部分,不用处理)
$result_del = array_diff($arr_old, $intersect);    //旧数据中需要删除的
$result_add = array_diff($arr_new, $intersect);    //新数据中需要增加的

//添加新增数据
if($result_add && is_array($result_add))
{
  foreach($result_add as $val)
  {
    $data_add = array('class_id'=>$id, 'lesson_id'=>$val);       //数据
    $bool_add = $this->lesson_classes->lessonClassesAdd($data_add);  //执行添加
  }
}

//删除需要清除的数据
if($result_del && is_array($result_del))
{
  foreach($result_del as $val)
  {
    $bool_del = $this->lesson_classes->lessonClassesDel($id, $val); //执行删除
  }
}
if($bool_add && $bool_del)
{
  $this->show_tips('操作成功!');
}
else
{
  $this->show_tips('操作失败!');
}
Copy after login

Features of this method: targeted addition and deletion of data, which is more secure than the first method

I hope this article will be helpful to everyone’s PHP programming design.

How to associate two or more data tables in PHP?

select u.qq,u.name,p.* from posts as p left join users as u on p.name=u.name

UPDATE multi-table update (reposted) (2008-05-12 15:29:04)
Reposted label: update multi-table update sql Category: PHP network programming
During development, the database is exchanged back and forth, and Some key syntaxes are different, which is a headache for developers. This article summarizes the usage of the Update statement in SQL Server, Oracle, and MySQL when updating multiple tables. I also tried SQLite The database has not been successful. I don’t know if it does not support multi-table updates or something.

In this example: We need to use the gqdltks and bztks field data in the table gdqlpj to update the data with the same field name in landleveldata. , the condition is to update when the GEO_Code field value in landleveldata 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])

Oracel Example: 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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/872373.htmlTechArticlePHP implements two methods of updating intermediate association table data, PHP two methods. This article shows the PHP implementation in the form of examples Two methods to update intermediate related table data. Share it with everyone for your reference...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

How to create tables with sql server using sql statement How to create tables with sql server using sql statement Apr 09, 2025 pm 03:48 PM

How to create tables using SQL statements in SQL Server: Open SQL Server Management Studio and connect to the database server. Select the database to create the table. Enter the CREATE TABLE statement to specify the table name, column name, data type, and constraints. Click the Execute button to create the table.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles