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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

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.

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.

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

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.

How to judge SQL injection How to judge SQL injection Apr 09, 2025 pm 04:18 PM

Methods to judge SQL injection include: detecting suspicious input, viewing original SQL statements, using detection tools, viewing database logs, and performing penetration testing. After the injection is detected, take measures to patch vulnerabilities, verify patches, monitor regularly, and improve developer awareness.

How to check SQL statements How to check SQL statements Apr 09, 2025 pm 04:36 PM

The methods to check SQL statements are: Syntax checking: Use the SQL editor or IDE. Logical check: Verify table name, column name, condition, and data type. Performance Check: Use EXPLAIN or ANALYZE to check indexes and optimize queries. Other checks: Check variables, permissions, and test queries.

How to add columns in PostgreSQL? How to add columns in PostgreSQL? Apr 09, 2025 pm 12:36 PM

PostgreSQL The method to add columns is to use the ALTER TABLE command and consider the following details: Data type: Select the type that is suitable for the new column to store data, such as INT or VARCHAR. Default: Specify the default value of the new column through the DEFAULT keyword, avoiding the value of NULL. Constraints: Add NOT NULL, UNIQUE, or CHECK constraints as needed. Concurrent operations: Use transactions or other concurrency control mechanisms to handle lock conflicts when adding columns.

See all articles