so i have this table in my html
<form method = "POST" action="admin_event.php">
<table class = "table" id="dynamic_admin_add_price">
<thead>
<th>Deadline </th>
<th>Register Date</th>
<th>Price</th>
<th></th>
<th></th>
</thead>
<tbody>
<tr>
<td>
<select name="admin_add_deadline[]" class="form-control">
<option value="early_bird">Early Bird</option>
<option value="middle">Middle</option>
<option value="normal">Normal</option>
</select>
</td>
<td><input type="text" name="admin_add_register_date[]" class="form-control registerDate" required></td>
<td><label>Rp.</label></td>
<td><input type="number" name="admin_add_price[]" class="form-control" value="" required></td>
<td><button type="submit" name="button_add_price" id="price" class="btn btn-success">Add more deadlines</button></td>
<!--<td><button type="button" name=button_remove_event_time id="remove_event-time" class="btn btn-danger" disabled>Remove</button></td>-->
</tr>
</tbody>
</table>
<button type="submit" class="btn btn-primary" name="admin_add_event_datas">Save</button>
</form>
And i have created the javascript so that the user can add as many rows as they want
$('#add_price').click(function(){
j++;
$('#dynamic_admin_add_price').append(
'<tr id="row2'+j+'">' +
'<td><select name="admin_add_deadline[]" class="form-control"><option value="early_bird">Early Bird</option><option value="middle">Middle</option><option value="normal">Normal</option></select></td>'+
'<td><input type="text" name="admin_add_register_date[]" class="form-control registerDate" required></td>' +
'<td><label>Rp.</label></td>' +
'<td><input type="number" name="admin_add_price[]" class="form-control" value="" required></td>' +
'<td><button type="submit" name="button_remove_event_time" id="'+j+'" class="btn btn-danger btn_remove2" required>Remove</button></td>' +
'</tr>');
});
$(document).on('click', '.btn_remove2', function(){
var button_id = $(this).attr("id");
$('#row2'+button_id+'').remove();
});
The adding rows work fine and the rest of the data can be inputted into the mysql, EXCEPT for the select fields,where it returns incorrectly and doesn't get any input at all from the non-static rows.
What i want to do is that if the user chose early bird, it gave the number 1 in the php, 2 for the middle option, and 3 for the normal ones, as it matches the id_deadline in another table that is used as the foreign key in the table i want to insert the data to. This is the snippet of the php script(i only put the one for the deadline):
if(isset($_POST['admin_add_deadline'])){
$deadline = $_POST['admin_add_deadline'];
$one = 1;
$two = 2;
$three = 3;
if($deadline == 'early_bird'){
$deadline = $one;
}
if($deadline == 'middle'){
$deadline = $two;
}
else{
$deadline = $three
}
}
and this is the foreach to insert the data
foreach ($register_date as $key => $value) {
$post_register_date = $db->real_escape_string($value);
$post_insert_price = $db->real_escape_string($register_price[$key]);
$post_deadline = $deadline[$key];
$query = "INSERT INTO register_time(id_event, id_deadline, register_date, price, created_at, status)
VALUES('$row_id_event','$post_deadline', '$post_register_date', '$post_insert_price', '$time', '$status')";
if(mysqli_query($db, $query)){
$succ = "New record created successfully";
}
else{
echo "Error: " . $query . "<br>" . mysqli_error($db);
}
}
Again, the problem is that if i have 3 rows generated and i clicked the submit button, the inserted value for the id_deadline column in the table will always be 3 for the first row, and the other 2 rows will return an error as the value is '' for the id_deadline in the insert query
Can you guys help me with this problem?Thank you, i hope my question is clear enough.
EDIT: Fixing the typos