簡體   English   中英

如何從數據庫中獲取項目的ID<select><option>標簽

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

你好。 我對更難的代碼有問題,但在這里我試圖簡化它只是為了問一個問題......我在數據庫中有 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';
 }
}
?>

為了更清楚

所以,你只需要像這樣編輯你的 $output 。 value="'.$row["model"] 到 value="'.$row["cars_id"] 更改為 cars_id 僅用於值,因此用戶可以看到名稱,但您會在表單提交時獲得 id。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM