2017-05-10 92 views
0

我有这些表中的数据库:插入到连接表的mysqli接力

country: id country 

和我有段表:

segments: id segment 

和第三台名为countrysegments有外键:国家ID相关与country.id相关的country.id和segment_id

countrysegments:  id country_id segment_id 

in 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表什么everycountry有选择的段

只是我需要查询的帮助和如何才能完成它感谢ü

+0

ü可以格式化代码 –

+0

你能告诉什么需要插入countrysegments表中。 或显示表的所期望的结果。 – Mack4Hack

+0

每个国家都必须有一个片段,所以我必须插入该国的ID和表countrysegments @ Mack4Hack – eddy

回答

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(); 
+0

我需要对每一个国家内部的段的ID选择一个段,所以我有myltiple国家和多个片段,所以我需要做一个for循环,但我不知道该怎么做,使用 – eddy

+0

你指的是贴阵列会是这样 阵列(“国家1” =>阵列(“segemtn1”,“分段2”),“COUNTRY2”阵列=>阵列( 'segement1', '分段2', '段3')) – Mack4Hack