Home Backend Development PHP Tutorial PHP实现更新中间关联表数据的两种方法_php技巧

PHP实现更新中间关联表数据的两种方法_php技巧

May 16, 2016 pm 08:36 PM
php Related table data method renew

本文以示例形式展示了PHP实现更新中间关联表数据的两种方法。分享给大家供大家参考之用。具体方法如下:

首先,中间关联表:这里的中间表只存表1的主键和表2的主键,即多对多的形式。
执行数据添加和删除为框架内部方法,不属于思路部分。

方法一:先全部删除旧数据,后添加新数据

$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

使用该方法弊端:大批量删除数据这块儿不安全,存在一定程度的安全隐患。

方法二:只添加需要的,只删要删的

//库中查出的旧数据:$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

该方法特点:有针对性的添加数据和删除数据,较第一种方法,安全性高

希望本文所述对大家的PHP程序设计有所帮助。

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles