I am trying to insert an array into a table within my database. I am dynamically adding inout fields so I can add team names. What I need to then do is insert those inputs into separate rows into the table but i either just get 'array' in the column or loads of empty fields.
Here is where I am at:
HTML for the form:
<div id="exercises">
<div class="team">
<input type="text" name="teamName[]" />
<button class="remove">x</button>
</div>
</div>
JQuery that handles the creating new inputs:
<script type="text/javascript">
$('#add_exercise').on('click', function() {
$('#exercises').append('<div class="team"><input type="text" name="teamName[]"><button class="remove">x</button></div>');
return false; //prevent form submission
});
$('#exercises').on('click', '.remove', function() {
$(this).parent().remove();
return false; //prevent form submission
});
</script>
Then I am wanting to submit that to the database so here is where I am at with that (no security in place and am aware its open to SQL injection).
<?php
// Config
include '../config.php';
// Get post data
$teamName = $_POST['teamName'];
$dashboardId = $_POST['dashboardId'];
// Check if the daashboard exists already
$sql = "INSERT INTO teams (team_name, dashboard_id) VALUES ('$teamName', '$dashboardId')";
if ($conn->query($sql) === TRUE) {
echo 'good';
}
?>
If i do a var_dump n test submits I get the following:
array(2) { [0]=> string(6) "team 1" [1]=> string(6) "team 2" }
So I would like to insert a row with the name 'team 1' and another with the name 'team 2'
Any help would be greatly appreciated as I am a little stuck...
name="teamName[], then$_POST['teamName']will be an array, so you need to loop it and insert them one by one (or in a batch-insert-query).