Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从<;中的数据库获取项目ID;选择>&书信电报;选项>;标签_Php_Html - Fatal编程技术网

Php 如何从<;中的数据库获取项目ID;选择>&书信电报;选项>;标签

Php 如何从<;中的数据库获取项目ID;选择>&书信电报;选项>;标签,php,html,Php,Html,你好。我有一个更难的代码问题,但在这里我试图简化它一点,只是为了问一个问题。。。我在数据库中有3个表:cars(cars\u id,model)、customers(customer\u id,name,姓氏)、sales(sales\u id,customer\u id,cars\u id)。我不知道如何在select option标签中获取所选选项的id。例如,对于BMW X6,我希望得到ID=2,并将该值放入SALES表的cars_ID列中,以避免数据冗余。这是关于关系的。cars表中的c

你好。我有一个更难的代码问题,但在这里我试图简化它一点,只是为了问一个问题。。。我在数据库中有3个表:cars(cars\u id,model)、customers(customer\u id,name,姓氏)、sales(sales\u id,customer\u id,cars\u id)。我不知道如何在select option标签中获取所选选项的id。例如,对于BMW X6,我希望得到ID=2,并将该值放入SALES表的cars_ID列中,以避免数据冗余。这是关于关系的。cars表中的cars\u id与SALES表中的cars\u 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';
 }
}
?>

销售表格
销售表格
出售 顾客 汽车 添加 $(文档).ready(函数(){ $(文档)。在('单击','添加',函数()上){ var html=''; html+=''; html+=''; html+=''; html+=''; html+='Usuń'; $('#item_table')。附加(html); var计数=0; $('.lp')。每个(函数(){ 计数=计数+1; $(此).text(计数); }); }); $(“#插入表格”)。在('submit',函数(事件){ event.preventDefault(); var form_data=$(this).serialize(); 如果(错误=“”) { $.ajax({ url:“insert.php”, 方法:“张贴”, 数据:表格数据, 成功:功能(数据) { 如果(数据=='ok') { $(“#item_table”).find(“tr:gt(0)”).remove(); $(“#error').html('Dane zostały wysłane'); } } }); } 其他的 { $('#error').html(''+error+''); } }); });

因此,您只需像这样编辑$output。 value=“”.$row[“model”]到value=“”.$row[“cars_id”]
更改为cars\u id仅用于值,这样用户可以看到名称,但您可以在表单提交时获得id。

那么,为什么不在value属性中使用$row[“id”]而不是$row[“model”]?
'.$row[“model”]。
是的,我可以将其更改为“.$row[“model”.”,但稍后在insert.php中,我需要将其放入第->:“:cars\u id=>$”行[“此处”][$count]并将其发送到数据库。我可以这样做吗?如何将它而不是名称attibute放在$\u POST[]中?因为我需要在insert.php中将汽车id发送到数据库$\u POST['key']可以通过html名称属性设置。也可以在ajax调用中更改此键。因此,如果您有类似的情况:您可以在post请求后使用$\u post['mySpecialKey']访问此值。这就是我要求的。我知道我可以在post请求后使用$\u post['mySpecialKey']访问此值但是我需要发送而不是名称attr value=$row[“cars_id”]。我需要发送到数据库$row[“cars_id”]。而不是名称attr。因为在选择选项中,我们在eg的选择标记中有名称,所以很容易从选择标记发送名称值,但在选择标记中有值attr而不是名称