获取任何2个输入值并保存到PHP MySQL

时间:2017-11-23 10:13:05

标签: php mysql forms

我有一个像这样的动态表格:

<form role="form" method="POST" enctype="multipart/form-data" id="applicationform" runat="server" onsubmit="return false">

    <div class="form-group col-md-6">
        <label>Company name</label>
        <input type="text" id="company_name_0" name="company_name[]" class="form-control" value="" placeholder="Enter company name">
    </div>
    <div class="form-group col-md-6">
        <label>Designation</label>
        <input type="text" id="designation_0" name="designation[]" class="form-control" value="" placeholder="Enter holding designation">
    </div>
    <div class="form-group col-md-12">
        <label>Responsibilities</label>
        <textarea type="text" id="responsibility_0" name="responsibility[]" value="" class="form-control" placeholder="Enter responsibilities handled by you."></textarea>
    </div>
    <div class="form-group col-md-5">
        <label>Start date</label>
        <input type="text" id="start_date_0" name="start_date[]" class="form-control" value="" placeholder="Enter job starting date" readonly='true'>
    </div>
    <div class="form-group col-md-4">
        <label>End date</label>
        <input type="text" id="end_date_0" name="end_date[]" class="form-control" value="" placeholder="Enter job ending date" readonly='true'>
    </div>
    <div class="form-group col-md-2">
        <div class="form-group">
            <label style="visibility: hidden;">&nbsp;None</label>
            <input type="checkbox" value="Tilldate" class="form-control" id="end_date_x" name="end_date_x[]"> I currently work here
        </div>
    </div>
    <div class="col-md-1 form-group">
        <div class="form-group">
            <label style="visibility: hidden;">&nbsp;Add</label>
            <button class="btn" type="button" id="add_more_exp" return="false"><i class="fa fa-plus"></i></button>
        </div>
    </div>
</form>

用户可以通过单击“+”图标追加字段。

在这里,我想保存以下任何值:输入(结束日期)或复选框(截止日期)到名为的相同字段:end_date。这是我将数据保存到数据库的PHP代码。

$company_name = $_POST['company_name'];
$designation = $_POST['designation'];
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];

$total_experience_item = count($company_name);
foreach ($company_name AS $key => $value) {
    $insert_experience_array = '';
    $insert_experience_array .= 'candidate_id = "' . $candidate_id . '"';
    $insert_experience_array .= ',company_name = "' . $value . '"';
    $insert_experience_array .= ',designation = "' . $designation[$key] . '"';
    $insert_experience_array .= ',start_date= "' . $start_date[$key] . '"';
    if (!empty($_POST['end_date'])){
        $insert_experience_array .= ',end_date = "' . $end_date[$key] . '"';
    } else{
        $end_date_x = $_POST['end_date_x'];
        $insert_experience_array .= ',end_date = "' . $end_date_x[$key] . '"';
    }


    echo $sql_experience = "INSERT INTO candidate_experience SET $insert_experience_array";
    $result_experience = mysqli_query($con, $sql_experience);
    if (!$result_experience) {
        $error = 'result_experience query failed';
    } else {
        $success = 'HOLA!';
    }
}

我正在运行上面提到的foreach,用于将每个数据保存到数据库中。除复选框值以外的所有数据都是保存的。有解决方案吗 怎么做?

0 个答案:

没有答案