Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.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 如何在数据库中存储选择选项的名称而不是值_Php_Jquery_Mysql - Fatal编程技术网

Php 如何在数据库中存储选择选项的名称而不是值

Php 如何在数据库中存储选择选项的名称而不是值,php,jquery,mysql,Php,Jquery,Mysql,我试图在MySQL数据库中存储select选项的名称,而不是值。当前代码在数据库中存储的是客户id而不是客户名称。我知道你们中的一些人会说,为什么不在name属性中使用$customer_result['name'],但如果我这样做了,那么我将无法使用id筛选记录 MYSQL和PHP <?php $select_customer = "select * from customer"; $select_customer_query =

我试图在MySQL数据库中存储select选项的名称,而不是值。当前代码在数据库中存储的是客户id而不是客户名称。我知道你们中的一些人会说,为什么不在name属性中使用$customer_result['name'],但如果我这样做了,那么我将无法使用id筛选记录

MYSQL和PHP

      <?php
      $select_customer = "select * from customer";
      $select_customer_query = mysqli_query($connection, $select_customer); // Customer Data

      if(isset($_POST['add-sale-btn'])) {
    
    $customer_name = mysqli_real_escape_string($connection, $_POST['customer_name']); 
    $customer_address = mysqli_real_escape_string($connection, $_POST['customer_address']); 


    
    $insert_sale = "insert into sale(customer_name, customer_address) values('$customer_name','$customer_address')";
    $insert_sale_query = mysqli_query($connection, $insert_sale);

}

      ?>

      <form method="post" action="">
      <div class="form-group">
      <select class="form-control" id="customer_name" name="customer_name">
      <option>Customer Name</option>
      <?php while($customer_result = mysqli_fetch_assoc($select_customer_query)) { ?>
      <option value="<?php echo $customer_result['id']; ?>"><?php echo $customer_result['name']; ?></option>
      <?php } ?>
      </select>
      </div>
       <div class="form-group" style="margin-top:10px;">
      <input type="submit" class="btn btn-primary" name="add-sale-btn" value="Save">  
      </div>
      </form>
按照中的排序,这个问题相当表明了一个问题

OP的原始设计围绕着能够在其
sale
表中存储进行销售的客户的姓名。但是,这种设计违反了的关系数据库原则,因为跨表的数据复制是不必要的,随着时间的推移,这可能会导致各种意外后果。根据我的评论:

[N] 艾姆斯一直在变化,如果“客户A”决定明天由“客户B”代替怎么办?如果不再有“客户A”,你会如何将销售与该客户联系起来


相反,解决方案是修改数据库模式,使
sale
表有一列存储客户的
ID
,以及其他订单属性,并将该客户
ID
作为外键正确链接回
customer
表。

这听起来像是XY问题-在这种特定情况下存储ID有什么问题。。。?为什么要存储可变属性而不是存储(表面上)不可变ID?警告:您完全可以使用参数化的预处理语句,而不是手动生成查询。它们由或提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行。这是参考
$select\u customer\u detail=“select*from customer where id={$\u POST['id']}”
它接受帖子中发送的内容并直接发送到数据库为什么我要在customer\u name列中存储id?我在value(select)属性中传递id,因为我需要通过ajax将其传递给服务器,然后过滤@esqew记录
      <script>
      $(document).ready(function(){
         $('#customer_name').on('change', function(){
             var customer_name = $('#customer_name').val();
             $.ajax({
               url: 'customer-detail.php',
               type: 'POST',
               data: {id : customer_name},
               success: function(customer_data){
                   $('#customer_detail').html(customer_data);
               }
             });
         }); 
      });
      </script> 
$select_customer_detail = "select * from customer where id={$_POST['id']}";
$customer_detail_query = mysqli_query($connection, $select_customer_detail);
$customer_detail_result = mysqli_fetch_assoc($customer_detail_query);

<div class="form-group">
<input type="text" class="form-control" name="customer_address" value="<?php echo $customer_detail_result['address']; ?>">
</div>