PHP实现更新中间关联表数据的两种方法,php两种方法
PHP实现更新中间关联表数据的两种方法,php两种方法
本文以示例形式展示了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('操作失败!'); }
使用该方法弊端:大批量删除数据这块儿不安全,存在一定程度的安全隐患。
方法二:只添加需要的,只删要删的
//库中查出的旧数据:$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('操作失败!'); }
该方法特点:有针对性的添加数据和删除数据,较第一种方法,安全性高
希望本文所述对大家的PHP程序设计有所帮助。
select u.qq,u.name,p.* from posts as p left join users as u on p.name=u.name
UPDATE多表更新(转)(2008-05-12 15:29:04)
转载标签:update多表更新sql 分类:php网络编程
在开发中,数据库来回换,而有些关键性的语法又各不相同,这是一件让开发人员很头痛的事情.本文总结了Update语句更新多表时在SQL Server,Oracle,MySQL三种数据库中的用法.我也试了SQLite数据库,都没成功,不知是不支持多表更新还是咋的.
在本例中: 我们要用表gdqlpj中的gqdltks,bztks字段数据去更新landleveldata中的同字段名的数据,条件是当 landleveldata 中的GEO_Code字段值与gdqlpj中的lxqdm字段值相等时进行更新.
SQL Server语法:UPDATE { table_name WITH ( [ ...n ] ) | view_name | rowset_function_limited } SET { column_name = { expression | DEFAULT | NULL } | @variable = expression | @variable = column = expression } [ ,...n ] { { [ FROM { } [ ,...n ] ] [ WHERE ] } | [ WHERE CURRENT OF { { [ GLOBAL ] cursor_name } | cursor_variable_name } ] } [ OPTION ( [ ,...n ] ) ]
SQL Server示例: update a set a.gqdltks=b.gqdltks,a.bztks=b.bztks from landleveldata a,gdqlpj b where a.GEO_Code=b.lxqdm
Oracle语法: UPDATE updatedtable SET (col_name1[,col_name2...])= (SELECT col_name1,[,col_name2...] FROM srctable [WHERE where_definition])
Oracel 示例: update landleveldata a set (a.gqdltks, a.bztks)= (select b.gqdltks, b.bztks from gdqlpj b where a.GEO_Code=b.lxqdm)
MySQL语法: UPDATE table_references SET col_name1=expr1 [, col_name2=expr2 ...] [WHERE where_definition]
MySQL 示例: update landleveldata a, gdqlpj b set a.gqdltks= b.gqdltks, a.bztks= b.bztks where a.GEO_Code=b.lxqdm

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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

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

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

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