Based on dropdown values I need to enter into its respective database table column

#1

> My html code in a php file

<td align="left">
    Assignment
</td>
<td>
    <div class="">
        <div class="form-group">
            <select class="form-control" id="distplace1" name="distplace1" onchange="gettaluka(this.value);" required="">
                <option value="0">
                    Select Assignment
                </option>
                <option value="1">
                    Cost Name
                </option>
                <option value="2">
                    Project Name
                </option>
                <option value="3">
                    Other Recovery
                </option>
            </select>
        </div>
    </div>
</td>
<td>
    <div class="">
        <div class="form-group">
            <select class="form-control" id="talukaplace" maxlength="10" name="talukaplace" required="">
                <option maxlength="10" value="0">
                    Select Values
                </option>
            </select>
        </div>
    </div>
</td>

My ajax function
function gettaluka(id)
{
if(id){
$.ajax({
type:‘POST’,
url:‘ajaxData.php’,
data:{‘id’:id},
success:function(data){ //console.log(html); return;
$(’#talukaplace’).html(data);
storetaluka();
}
});
}else{
$(’#talukaplace’).html(‘Select Taluka’);

    }

}

The file where ajax function is called. i.e., ajaxData.php

$dist_id = $_POST["id"];
if ($dist_id == '1') {
    ?>
        <html>
              <?php
$query   = "select cost_name from cost_centers";
    $results = mysqli_query($conn, $query);
    if (mysqli_num_rows($results) > 0) {
        echo '<option value="" maxlength="100">Select Taluka</option>';
        while ($row = mysqli_fetch_assoc($results)) {
            echo '<option value="' . $row['cost_name'] . '">' . $row['cost_name'] . '</option>';
        }
    } else {
        echo '<option value="">Select Values</option>';
    }
}
;
elseif ($dist_id == '2') {
    $query   = "select proj_name from project";
    $results = mysqli_query($conn, $query);
    if (mysqli_num_rows($results) > 0) {
        echo '<option value="">Select Project</option>';
        while ($row = mysqli_fetch_assoc($results)) {
            echo '<option value="' . $row['proj_name'] . '">' . $row['proj_name'] . '</option>';
        }
    } else {

    }
}
#2

If I select option value 1 then its respective value should enter into db table’s column say cost_names i.e., Insert into data(id, names, cost_names, project_names,…) values(1, xyz, cost1, …).

If I select option value 2 then its respective value must get inserted into same database table say proj_name i.e., Insert into data(id, names, cost_names, project_names,…) values(2, xyz, ’ ', ‘proj1’);
How can I do in php? Any help will be appreciated. I am badly stuck here. I am able to insert but it is getting inserted into project_names field only.

#3

there’s no INSERT within your code.

#4

Sorry forgot to show insert query

$costcntr = $_POST['distplace1']; $project_id = $_POST['talukaplace'];
$query = "INSERT INTO data (`id`,`cost_names`,`project_names`,`dept_id`,`date_added`,`date_modified`)

VALUES(’".$id."’, ‘".$costname."’, ‘".$proj_name."’, ‘".$dept_id."’, NOW(), NOW())";

#5

Can you please help me with it. @chorn

#6

Can anyone give me some idea on this?

#7

make a whitelist of the columns

$whitelist = ['cost_names', 'project_names'];

get all values that are send for the whitelist

$values = array_intersect_key($_POST, array_flip($whitelist));

and use these for the statement

$query = 'insert into table('.implode(',', $array_keys($values)).') values('.implode(',', array_fill(0, count($values), '?')).')';