Home  >  Article  >  Backend Development  >  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

WBOY
WBOYOriginal
2016-07-13 10:19:46922browse

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('操作失败!');
}

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('操作失败!');
}

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

Teach sql statement how to update related table fields

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 ( 2232f73b0ad874487a5184a323681262 [ ...n ] ) | view_name | rowset_function_limited } SET { column_name = { expression | DEFAULT | NULL } | @variable = expression | @variable = column = expression } [ ,...n ] { { [ FROM { 7c96332c9c67603919ac812eb38f214c } [ ,.. .n ] ] [ WHERE 85b8a6ef03ad6891bf08d8599a7701cb ] } | [ WHERE CURRENT OF { { [ GLOBAL ] cursor_name } | cursor_variable_name } ] } [ OPTION ( 20f279aea0a51c1afb847cac5868e24f [ ,...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:
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