简体   繁体   English

如何从数据库中获取项目的ID<select><option>标签

[英]How to get ID of item from database in <select><option> Tag

Hello.你好。 I have a problem with the more difficult code but here I tried to simplify it a bit just to ask a question ... I have 3 tables in database: cars(cars_id,model), customers(customer_id,name,surname), sales(sales_id,customer_id,cars_id).And I don't know how to get the ID for the selected option in the select option tag.我对更难的代码有问题,但在这里我试图简化它只是为了问一个问题......我在数据库中有 3 个表:cars(cars_id,model),customers(customer_id,name,surname),sales (sales_id,customer_id,cars_id)。而且我不知道如何在选择选项标签中获取所选选项的 ID。 For example, for BMW X6 I want to get to ID = 2 and put this value in the cars_id column in the SALES table to avoid data redundancy.例如,对于 BMW X6,我想获得 ID = 2 并将此值放在 SALES 表的 cars_id 列中以避免数据冗余。 It's about relation.这是关系。 cars_id in the CARS table is the same as cars_id in the SALES table. CARS 表中的cars_id 与SALES 表中的cars_id 相同。 But I want to do this using PHP MySQL.但我想使用 PHP MySQL 来做到这一点。

<?php
//index.php

$connect = new PDO("mysql:host=localhost;dbname=store", "root", "");
function fill_unit_select_box($connect)
{ 
 $output = '';
 $query = "SELECT * FROM cars";
 $statement = $connect->prepare($query);
 $statement->execute();
 $result = $statement->fetchAll();
 foreach($result as $row)
 {
  $output .= '<option  value="'.$row["model"].'">'.$row["model"].'</option>';
 }

 return $output;
}

?>
<html>
 <head>
  <title>Sales form</title>
  <script src="jquery-3.5.0.min.js" defer></script>
  <link rel="stylesheet" href="bootstrap.min.css" />
  <script src="jquery-3.5.0.min.js"></script>
 </head>
 <body>
  <div class="container">
   <h3 align="center">Sales form</h3><br />
   <form method="post" id="insert_form">
    <div class="table-repsonsive">
     <span id="error"></span>
     <table class="table table-bordered" id="item_table">
    <tr>
        <th >sales</th>    
        <th >customer</th>
        <th >cars</th>
        <th><button type="button" name="add" class="btn btn-success btn-sm add"><span class="glyphicon glyphicon-plus">Add</span></button></th>
    </tr>
     </table>
     <div align="center">
      <input type="submit" name="submit" class="btn btn-info" value="Send" />
     </div>
    </div>
   </form>
  </div>
 </body>
</html>


<script>
$(document).ready(function(){

 $(document).on('click', '.add', function(){
    var html = '';
    html += '<tr>';
    html += '<td class="lp"></td>';
    html += '<td><input type="text" name="customers_name[]"></td>';
    html += '<td><select style="backbround-color:white;"name="cars_name[]" class="form-control size_id"><option value=""><?php echo fill_unit_select_box($connect); ?></option></select></td>';
    html += '<td><button type="button" name="remove" class="btn btn-danger btn-sm remove"><span class="glyphicon glyphicon-minus">Usuń</span></button></td></tr>';
    $('#item_table').append(html);
    var count=0;
    $('.lp').each(function(){
      count=count+1;
      $(this).text(count);
  });
 });


 $('#insert_form').on('submit', function(event){
  event.preventDefault();

  var form_data = $(this).serialize();
  if(error == '')
  {
   $.ajax({
    url:"insert.php",
    method:"POST",
    data:form_data,
    success:function(data)
    {
     if(data == 'ok')
     {
      $('#item_table').find("tr:gt(0)").remove();
      $('#error').html('<div class="alert alert-success">Dane zostały wysłane</div>');
     }
    }
   });
  }
  else
  {
   $('#error').html('<div class="alert alert-danger">'+error+'</div>');
  }
 });

});

</script>   




<?php
//insert.php;
session_start();

if(isset($_POST["customers_name"]))
{
 $connect = new PDO("mysql:host=localhost;dbname=store", "root", "");
 for($count = 0; $count < count($_POST["customers_name"]); $count++)
 {  
    $query = "INSERT INTO sales
    (customer_id,cars_id) 
    VALUES (:customer_id,:cars_id)
    ";
    $statement = $connect->prepare($query);
    $statement->execute(
    array(
        ':customer_id'  => $_POST["customers_name"][$count],    
        ':cars_id'  => $_POST["cars_name"][$count],
   )
  );
 }
 $result = $statement->fetchAll();
 if(isset($result))
 {
  echo 'ok';
 }
}
?>

为了更清楚

So, you just have to edit your $output like this.所以,你只需要像这样编辑你的 $output 。 value="'.$row["model"] to value="'.$row["cars_id"] change to cars_id only for value, so the user can see the name but you get id on form submit. value="'.$row["model"] 到 value="'.$row["cars_id"] 更改为 cars_id 仅用于值,因此用户可以看到名称,但您会在表单提交时获得 id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM