选择并使用php将复选框插入mysql

时间:2019-03-20 12:26:13

标签: jquery ajax

这是我的表单,用于添加客户可用的服务

this is my form to add the services the customer availed

此图片是我来自数据库的表格

this image is my table from database

这是来自我的PHP代码的复选框显示服务

here is from my PHP code to display services with the checkboxes

问题是:如何将所选的复选框插入数据库表。该图显示了我的表,可以在其中保存所选数据库

如果我使用jQuery,如何应用它并插入到表中?

  

此代码来自我的create_serviceAvailed.php

<?php include('assets/session.php'); ?>
<?php include('php/insert_serviceAvailed.php'); ?>

<!DOCTYPE html>
<html class="no-js" lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="x-ua-compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Add Availed Service</title>
    <?php include('assets/style.php') ?>
  </head>
<body>
    <?php include('assets/topbar_index.php'); ?>
    <div class="header">
        <h2>Add Service Availed</h2>
    </div>
    <form method="post">
      <div class="grid-container">
        <div class="small-6 cell">
            <label> Reciept No.
              <input type="number" name="reciept_no" placeholder="Reciept No" required>
            </label>
          </div>
          <div class="small-12 cell">
              <label> Customer ID
                <select name="customer_id">
                 <?php 
                    $sql = "SELECT * FROM Customer";
                    $result = mysqli_query($db, $sql);
                    if(mysqli_num_rows($result)){
                      while($row = mysqli_fetch_array($result)){
                  ?>
                  <option value="<?php echo $row['customer_id'];?>"><?php echo $row['first_name'];?></option>
                  <?php
                      }
                    }
                  ?>
                </select>
              </label>
            </div>
        <div class="checkbox">
            <?php $result1 = mysqli_query($db, "SELECT * FROM Services");
            while ($row1 = mysqli_fetch_array($result1)) {?>
            <input type="checkbox" class="get_value" name="<?php echo $data[$count] ?>"  value="<?php echo $ServiceID; ?>" ><?php echo $row1['Serv_Name']; ?>
            <?php
            }?>
        </div>
      </div>
      <button class="button expanded" type="submit" name="submit" id="submit">Save</button>
    </form>
    <!-- Scripts -->
    <script src="Foundation/js/vendor/jquery.js"></script>
    <script src="Foundation/js/vendor/what-input.js"></script>
    <script src="Foundation/js/vendor/foundation.js"></script>
    <script src="Foundation/js/app.js"></script>
</body> 
</html>

此代码来自insert_serviceAvailed.php

<?php
$user_id = $_SESSION['username'];

// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'beautysalon');

//check connection
if($db === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

// adding contact
if (isset($_POST['submit'])) {
  // receive all input values from the form
  $reciept_no = $_POST['reciept_no'];
  $customer_id = $_POST['customer_id'];
  $Services_checked = $_POST['Services_checked'];


  $sql = "INSERT INTO `Services_availed` (`reciept_no`, `customer_id`, `Services_checked`, `timestamp`) VALUES ('$reciept_no', '$customer_id', '$Services_checked', CURRENT_TIMESTAMP);";
    $result = mysqli_query($db, $sql);
    if($result == true){
      header('Location:service_availed.php');
    }else{
      echo "Something went wrong";
    }  
}

2 个答案:

答案 0 :(得分:0)

将复选框的名称设置为name="<?php echo $data[$count] ?>",现在将此变量数据存储在mysql中

答案 1 :(得分:0)

请再澄清一点您的问题。您想在这里使用jquery做什么?

编辑: 下次,请发布您的真实代码,而不是任何形式的屏幕截图,谢谢。

答案: 在“添加服务可用”页面中,您必须给表单一个动作以将所有输入发送到该表单。在这里是这样的:

<form action ="insert_serviceAvailed.php" method="post">
PUT HERE ALL INPUTS
</form>

在insert_serviceAvailed.php上,您可以像这样简单地访问所有变量: $ _POST ['Services_checked'] =做点什么。

您的问题与jquery无关。

亲切的问候。