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条回答 默认 最新

  • duanemei2194 2018-11-29 01:34
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!