简体   繁体   English

获取2个输入值中的任何一个并保存到PHP MySQL中的相同数据库字段中

[英]Getting any of the 2 input value and save to same database field in PHP MySQL

I've a dynamic form like this: 我有这样的动态形式:

<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>

User can append field by clicking '+' icon. 用户可以通过单击“ +”图标来附加字段。

Here, I want to save any of the values from: Input (End date) Or checkbox (Till date) to same field named: end_date. 在这里,我要保存以下任意值:输入(结束日期)或复选框(至日期)到名为end_date的相同字段。 Here is my PHP code to save data to database. 这是我的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!';
    }
}

I'm running above mentioned foreach for saving each data into the database. 我正在上面提到的foreach中运行,用于将每个数据保存到数据库中。 All the datas except checkbox value is saving. 除复选框值外的所有数据都将保存。 Any solution ? 有什么办法吗? How to do this? 这个怎么做?

You need to fetch the checkbox value via $_POST : 您需要通过$_POST获取复选框值:

$till_date = $_POST['end_date_x'];

Then, $till_date will be an array that you can access within the foreach loop. 然后, $till_date将是您可以在foreach循环中访问的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM