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

你好。我遇到了更困难的代码的问题,但是在这里我试图简化一下,只是问一个问题...数据库中有3个表:cars(cars_id,model),customers(customer_id,name,surname),sales (sales_id,customer_id,cars_id)。而且我不知道如何在选择选项标签中获取所选选项的ID。例如,对于BMW X6,我想获取ID = 2并将此值放在SALES表的cars_id列中,以避免数据冗余。关于关系。 CARS表中的cars_id与SALES表中的cars_id相同。但是我想使用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';
 }
}
?>

To make it more clear