当我添加另一个表时,如何更改表中列的值?

How do I change the row value of a column in a table when I add another table? How do you ask for help? I have two tables in the database The first table is called Drug It consists of three columns: Sample Table I

// TABLE Drug 
DROP TABLE IF EXISTS `Drug`;
CREATE TABLE IF NOT EXISTS `Drug` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`brId` text NOT NULL,
`nameDrug` text NOT NULL,
 PRIMARY KEY (`id`)
 )

The second table is named brand Sample Table II

       // TABLE brand
   DROP TABLE IF EXISTS `brand`;
   CREATE TABLE IF NOT EXISTS `brand` (
   `idBrand` int(11) NOT NULL AUTO_INCREMENT,
   `brandName` text NOT NULL,
  `theUse` text NOT NULL,
   PRIMARY KEY (`idBrand`)
   )

What I need is when you add a row in the brand table, brId is updated in the Drug table to the new idBrand by id in the drug table that was sent I've done the following code because it does not work

   <?php
    require_once('include/config.php');

    $id = $_POST['id'];

   $brandName = $_POST['brandName'];

   $theUse = $_POST['theUse'];

    $query = "INSERT INTO brand 
    (brandName,theUse)VALUES('".$brandName."','".$theUse."');";

     $insertBrand = mysqli_query($con,$query);           
                  if($insertBrand)
                    {
                $updatDrug = "UPDATE  `drug` SET  `brId` = new.idBrand WHERE `id` = '".$id."' ;";
                $resultEnd = mysqli_query($con,$updatDrug);
    if($resultEnd){
     $result = 'OK';
     echo json_encode($result);
     }else{
     $resultno = 'NO';
    echo json_encode($resultno);
       }
    }                                                   

mysqli_close($con);
?>

After the INSERT, use mysqli_insert_id as the value for brId.

$br = mysqli_insert_id($con);
$updatDrug = "UPDATE drug SET brId = :brid WHERE id = :id";
$stmt = $con->prepare($updatDrug);
$stmt->bind_param('ii', $br, $id);
$stmt->execute();
$stmt->close();

And please avoid SQL INJECTION

Try transaction commit, here is an example

<?php
$db = new mysqli("localhost","root","","test"); //连接数据库

$db->autocommit(false); //设置为非自动提交——事务处理

$sql1  = "INSERT INTO `test`.`test1` (`name` )VALUES ('1' )";

$result1 = $db->query($sql1);

$sql2  = "INSERT INTO `test`.`test2` (`a` )VALUES ('1')";

$result2 = $db->query($sql2);

if ($result1 && $result2) {

$db->commit();  //全部成功,提交执行结果

echo '提交';

} else {

$db->rollback(); //有任何错误发生,回滚并取消执行结果

echo '回滚';

}

$db->autocommit(true); 

$db->close();

?>