drvvvuyia15070493
2018-11-29 01:26
浏览 24
已采纳

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

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);
?>
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题