duanmao2774 2017-05-10 11:37
浏览 23

插入连接表mysqli oop

i have these tables in database:

country:    id    country

and i have segment table:

segments:  id   segment

and the third table named countrysegments have foreign key: country id related to country.id and segment_id related to country.id

countrysegments:     id   country_id    segment_id

in index.php i have this:

 <div class="col-md-12">
            <table id="tbl" class = "table table-bordered table-responsive  ">
            <thead>

                            <th class="text-center">country</th>

                            <th class="text-center">segments</th>
            </thead>
            <tbody>
            <?php

                require 'class.php';

                $conn = new db_class();
                $read = $conn->select();
                                    $test = $conn->read();

                 while($fetch = $read->fetch_array()){                      
                                  ?>
    </td><td><select name="" id="">
            <?php
            $reads = $conn->read();

            while($fetch = $reads->fetch_array()){
              ?>

            <option><?php echo $fetch['segment']?></option>

            <?php   }?>

    </select>
  </td>  </tr>

                             <?php       }



        ?>




        </tbody>
    </table>
             <form action="activate.php" method="post">
             <button class="btn btn-danger" name="save">Save</button>

i have inner join the three tables like this in class.php:

           public function selectFrom(){
        $stmt = $this->conn->prepare("select * from countrysegments 
   inner join country on countrysegments.country_id  = country.id
   inner join segments on countrysegments.segment_id = segments.id") or die($this->conn->error);
        if($stmt->execute()){
            $result = $stmt->get_result();
            return $result;
        }
    }

the question is that i have to insert into countrysegment table what everycountry have choose as segment

just i need help of the query and how might it done thank u

  • 写回答

1条回答 默认 最新

  • duanjiao5082 2017-05-10 12:46
    关注

    Assuming that you have posted values properly, try this following code

    $country_id = $_REQUEST['country_id'];
    $segment_id = $_REQUEST['segment_id'];
    
    $stmt = $conn->prepare("INSERT INTO countrysegments (country_id, segment_id) VALUES (?, ?)");
    $stmt->bind_param("ii", $country_id, $segment_id);
    
    $stmt->execute();
    $stmt->close();
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP