my table looks like,
id | heading | description
1 | head1,head2 | des1,des2
and i want it to be like this,
id | heading | description
1 | head1 | des1
2 | head2 | des2
Here I am added values into table with ‘add more’ feature in frontend UI. These vales are fetched using array. after applying ‘implode’ method i could store it into a table. But unfortunately that values are stored with comma separated manner in a same row . how can i store these comma separated array values into different row?
UI:
<p>Add technical details below</p>
<label class="label"> <strong> Warishan Names <strong> </label>
<div class="table-responsive">
<table id="form_tablee" class="table table-bordered">
<tr>
<!-- <th>S. No</th> -->
<th>heading</th>
<th>Description</th>
</tr>
<tr class='case'>
<!-- <td><span id='snum'>1.</span></td> -->
<td><input class="form-control" type='text' name='heading[]'/></td>
<td><input class="form-control" type='text' name='description[]'/></td>
<input type="hidden" class="prodid" name="id">
</tr>
</table>
<button type="button" class='btn btn-success addmoree'>+ add new field</button> <br>
</div>
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css"> -->
<script>
$(document).ready(function(){
$(".addmoree").on('click', function () {
var count = $('table tr').length;
var data = "<tr class='case'>";
data += "<td><input class='form-control' type='text' name='heading[]'/></td> <td><input class='form-control' type='text' name='description[]'/></td></tr>";
$('#form_tablee').append(data);
count++;
});
});
</script>
function:
<?php
$heading = $_POST['heading'];
$description = $_POST['description'];
$id = $_POST['id'];
if (isset($_POST["heading"]) && is_array($_POST["heading"])){
$heading = $_POST['heading'];
$heading_str =implode(",",$heading);
}
if (isset($_POST["description"]) && is_array($_POST["description"])){
$description = $_POST['description'];
$description_str =implode(",",$description);
}
$status="1";
$conn = $pdo->open();
try{
$stmt = $conn->prepare("INSERT INTO tech (heading ,description ,pro_id) VALUES (:heading , :description ,:pro_id)");
$stmt->execute(['heading'=>$heading_str,'description'=>$description_str,'pro_id'=>$id]);
$_SESSION['success'] = ' successfully';
}
catch(PDOException $e){
$_SESSION['error'] = $e->getMessage();
}
$pdo->close();
header('location: products.php');
?>