i have this code working fine with single input data
index.php
<form method="post" id="insert_form">
<div class="table-repsonsive">
<span id="error"></span>
<table class="table table-bordered" id="item_table">
<thead>
<tr>
<th>Customer Name <td><select name="item_name[]" class="form-control item_name"><option value="">Select Name</option><?php echo fill_select_boxing($connect, "10"); ?></select></td></th>
</tr>
<tr>
<th>Category</th>
<th>Sub Category</th>
<th><button type="button" name="add" class="btn btn-success btn-xs add"><span class="glyphicon glyphicon-plus"></span></button></th>
</tr>
</thead>
<tbody></tbody>
</table>
<div align="center">
<input type="submit" name="submit" class="btn btn-info" value="Insert" />
</div>
</div>
</form>
<script>
$(document).ready(function(){
var count = 0;
$(document).on('click', '.add', function(){
count++;
var html = '';
html += '<tr>';
html += '<td><select name="item_category[]" class="form-control item_category" data-sub_category_id="'+count+'"><option value="">Select Category</option><?php echo fill_select_box($connect, "0"); ?></select></td>';
html += '<td><select name="item_sub_category[]" class="form-control item_sub_category" id="item_sub_category'+count+'"><option value="">Select Sub Category</option></select></td>';
html += '<td><button type="button" name="remove" class="btn btn-danger btn-xs remove"><span class="glyphicon glyphicon-minus"></span></button></td>';
$('tbody').append(html);
});
});
</script>
insert.php
<?php
if(isset($_POST["item_category"]))
{
include('database_connection.php');
for($count = 0; $count < count($_POST["item_category"]); $count++)
{
$data = array(
':item_name' => $_POST["item_name"][$count],
':item_category_id' => $_POST["item_category"][$count],
':item_sub_category_id' => $_POST["item_sub_category"][$count]
);
$query = "
INSERT INTO mapping
(item_name, item_category_id, item_sub_category_id)
VALUES (:item_name, :item_category_id, :item_sub_category_id)
";
$statement = $connect->prepare($query);
$statement->execute($data,);
}
echo 'ok';
}
?>
when i try to add more than one category input in my web, the code just insert the first row to mysql
data that i have is :
- customer : CustA
- item 1 : chair
- item 2 : table
i want to insert those data like this :
| Column A | Column B |
|---|---|
| custA | chair |
| custA | table |