简体   繁体   中英

Update multiple rows Foreign Key with select form

I have 3 tables :

files(file_id,name,etc..),
groups(group_id,name,etc...) and

file_group(file_id,group_id)=> Using foreign key here with the primary keys from both tables(files and groups).

My issue is : I'm having trouble to make an update on my table file_group .

Error message : Integrity constraint violation : 1062 Duplcate entry '40-6' for key PRIMARY....

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. 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(); 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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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