如何将多个文本框和复选框值插入表

时间:2014-10-17 08:15:11

标签: php mysql

我有文本框来添加service.also用户可以在按钮(添加字段)点击时添加多个文本框。有复选框显示主要服务。我想要做的是,当用户添加多个文本框时,选择复选框和submit.it应该逐行插入到mysql表中。这是我的代码。

**add_sub_services.php**

//to increment textfields

    <script type="text/javascript">
    function add_fields() {
       var d = document.getElementById("content");

       d.innerHTML += "<br /><span>Sub Service : <input type='text' size='50' name='subservice[]'  value='' required /><br />";
    }
    </script>

    <form id="" action="db_sql/db_add_sub_service.php" method="post">
    //display main services in checkboxes.it's from another mysql table
         <?php
          include_once '../include/class.user.php';
         $obj=new User();
          $result=$obj->user_services();
          while($value=  mysqli_fetch_assoc($result)){
          $sid=$value['sid'];
            echo '<input type="checkbox" name="servicename['.$value['sid'].']" value="'.$value['servicename'].'" />';
            echo'<input type="text" name="services" disabled="disabled" value="'.$value['servicename'].'" size="30" "/>';
            echo '</br></br> ';
            }

        ?>

//text field to sub service.user can add multiple textfields
        div id="content">
                        <span>Sub Service : <input type="text" size="50" name="subservice[]" value="" required />
                        </br>
                        </div>

                </div>
                <input type="button" id="more_fields" onclick="add_fields();" value="Add field" />
                </div>
              <input type="submit"  value="Add Sub Services" />
                </div>

        </form>

db_add_sub_service.php

<?php
require_once 'db_config.php';
require_once '../../include/class.user.php';

$obj=new User();
$obj->add_sub_service();
?>

class.user.php

public function add_sub_service()
    {
    $servicename=$_POST['servicename'];
    $subservice=$_POST['subservice'];
    $sql20="INSERT INTO sub_services SET servicename='".$servicename[1]."',subservice='".$subservice[1]."'";
    $result = mysqli_query($this->db,$sql20) or die(mysqli_connect_errno()."Data cannot inserted");
            return $result;
    }

现在它只插入第一个复选框值(第一个主要服务)only.text字段值为空。

sub_services表

sub_services table

add_sub_services.php界面

add_sub_services.php interface

0 个答案:

没有答案