简体   繁体   English

使用选择表单更新多行外键

[英]Update multiple rows Foreign Key with select form

I have 3 tables : 我有3张桌子:

files(file_id,name,etc..), 文件(文件ID,名称等),
groups(group_id,name,etc...) and 组(group_id,名称等)和

file_group(file_id,group_id)=> Using foreign key here with the primary keys from both tables(files and groups). file_group(file_id,group_id)=>在此使用外键和两个表(文件和组)的主键。

My issue is : I'm having trouble to make an update on my table file_group . 我的问题是:我无法对表file_group进行更新。

Error message : Integrity constraint violation : 1062 Duplcate entry '40-6' for key PRIMARY.... 错误消息:违反完整性约束:1062重复键“ PRIMARY”的条目“ 40-6”。

I know why it's telling me that but I still cannot make it work. 我知道为什么它告诉我,但我仍然无法使它起作用。

The code is: 代码是:

 <form method="post" action="edit.php"> <select multiple="multiple" name="groups" class="form-control" > <option value=1>Admin</option> <option value=2>project_1</option> <option value=11>Project_Bio</option> <option value=12>Project_3</option> <option value=20>Project_Off</option> <option value=22>Project_zed</option> </select> </form> <?php $id = 40; if(!empty($_POST)){ //signleton to get the table where i want to make the update $fc = $app->getTable('fc'); $new_grps = $_POST['groups']; foreach($new_grps as $k => $new_grp){ $fc->update($id,[ 'group_id' => $new_group ]); } } ?> 

In my table file_group , a file can have 2 groups. 在我的表file_group ,一个文件可以有2个组。 So, the code is trying to make the same update with the same data. 因此,代码正在尝试使用相同的数据进行相同的更新。 For me it seems like the loop doesn't work! 对我来说,循环似乎不起作用!

Any tips? 有小费吗?

The Var_dump(); Var_dump(); gives the following: 给出以下内容:

array(2){
   [0]=>string(1)"11" 
   [1] =>string(2)"20"
}

Data Table structures : 数据表结构:

--
-- Table structure for file
--
CREATE TABLE files (
  file_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
  title VARCHAR(255) NOT NULL,
  description TEXT DEFAULT NULL,
  owner VARCHAR(255) DEFAULT NULL,
  date DATETIME DEFAULT NULL,
  last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY  (file_id),
  KEY idx_title (title)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `groups`
--

CREATE TABLE groups (
  group_id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
  name VARCHAR(25) NOT NULL,
  last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY  (group_id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Table structure for table `file_group`
--

CREATE TABLE file_group (
  file_id SMALLINT UNSIGNED NOT NULL,
  group_id TINYINT UNSIGNED NOT NULL,
  last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (file_id, group_id),
  CONSTRAINT fk_file_group_file FOREIGN KEY (file_id) REFERENCES files (file_id) ON DELETE RESTRICT ON UPDATE CASCADE,
  CONSTRAINT fk_film_group_group FOREIGN KEY (group_id) REFERENCES groups (group_id) ON DELETE RESTRICT ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

It such cases you have to use database transactions. 在这种情况下,您必须使用数据库事务。 Once you start a transaction, you update the foreign key columns and then commit. 一旦开始事务,就更新外键列,然后提交。

<?php
$id = 40;
if(!empty($_POST)){
    //signleton to get the table where i want to make the update
    $fc = $app->getTable('fc');
    $new_grps = $_POST['groups'];
    updateForeign($new_goups,$fc); 
}

function updateForeign($new_groups,$fc){
    //$dbh is your database connection
    $dgh = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);
    $dbh->beginTransaction();
    try{
        foreach($new_grps as $k => $new_grp){
                $fc->update($id,[
                    'group_id' => $new_group
                ]);
        }   
    }
    catch(\Exception $e){
        $dbh->rollback();
        return false;
    }
    $dbh->commit();
    return true;
}

?>

Remember, you have to get the PDO database connection instance in the function updateForeign() . 记住,您必须在函数updateForeign()获取PDO数据库连接实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM