Reputation: 3
Please help. I know this is simple but I have been at it and cannot get it right.
I have a form with a field where the user can select multiple values. The values are separated by a comma.
I want to submit the form, and insert individual rows for each value in the field.
I am able to explode the values in the string, by the "," and end up with each individual value; but I cannot get the SQL to insert for each value.
Example: User selects three values: A,B,C I can explode and echo three values, A then B Then C I want the form to submit the fields three values as row 1 field.value = A row 2 field.value = B row 3 field.value = C.
(the user is filling out a form for taking leave. They select multiple dates and I want one record for each date in the field, the dates are separated by commas)
$dates = (explode(",",$str));
foreach ($dates as $value) {
$insertSQL = "INSERT INTO tbl_test (Date) VALUES ($value)";
}
any help would be so appreciated! thank you
Upvotes: 0
Views: 97
Reputation: 1743
if you have multiple value to submit.
<form action="anything">
<input type="text" name="param[]" value="A">
<input type="text" name="param[]" value="B">
<input type="text" name="param[]" value="C">
<input type="submit" value="Submit"/>
</form>
in php
$data = $_POST["param"];
if(!empty($data)){
for($i = 0; $i < count($data); $i++) {
$data[$i]; // here is A,B,C
//Your Query to insert
}
}
Upvotes: -1
Reputation: 10148
You are preparing your query but you don't seem submitting it.. try this
$dates = (explode(",",$str));
foreach ($dates as $value)
{ $insertSQL = "INSERT INTO tbl_test (Date) VALUES ('$value')";
mysqli_query(your_db_connection_object, $insertSQL);
}
Upvotes: 0