Reputation: 17
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 :
i want to insert those data like this :
Column A | Column B |
---|---|
custA | chair |
custA | table |
Upvotes: 0
Views: 257
Reputation: 370
You can bind your variables in the for loop, then execute. There are also options to bindParam you can add to account for different data types. Look in the PHP documentation online for bindParam options.
if(isset($_POST["item_category"]))
{
include('database_connection.php');
$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);
for($count = 0; $count < count($_POST["item_category"]); $count++)
{
$statement->bindParam(':item_name', $_POST["item_name"][$count]);
$statement->bindParam(':item_category_id', $_POST["item_category"][$count]);
$statement->bindParam(':item_sub_category_id', $_POST["item_sub_category"][$count]);
$statement->execute();
} // end for
echo 'ok';
} // end if
Upvotes: 1