插入连接表mysqli oop

时间:2017-05-10 11:37:42

标签: php

我在数据库中有这些表:

country:    id    country

我有段表:

segments:  id   segment

并且名为countrysegments的第三个表具有外键:与country.id相关的国家/地区ID和与country.id相关的segment_id

countrysegments:     id   country_id    segment_id
index.php中的

我有这个:

 <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>

我在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;
        }
    }

问题是我必须在countrysegment表中插入每个国家/地区选择的段

我需要查询的帮助以及如何完成,谢谢你

1 个答案:

答案 0 :(得分:0)

假设您已正确发布值,请尝试以下代码

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