1

I have a list of days in my database created with the SET datatype.

SET('Mon','Tue','Wed','Thr','Fri','Sat','Sun')

I want a user to be able to select multiple days to put into the database using a multiple select dropdown:

<select name='days' id='days' size=4 multiple>
<option name=Mon value=Mon> Mon </option>
<option name=Tue value=Tue> Tue </option>
<option name=Wed value=Wed> Wed </option>
<option name=Thr value=Thr> Thr </option>
<option name=Fri value=Fri> Fri </option>
<option name=Sat value=Sat> Sat </option>
<option name=Sun value=Sun> Sun </option>
</select>

How can I grab the data from this dropdown to insert into my mysql query?

Right now my query is like this:

$sql2 = " UPDATE tblFacilityHrsDateTimes SET `startEventDate`='{$_POST['startEventDate']}',`endEventDate`='{$_POST['endEventDate']}', `startTime`='{$_POST['startTime']}',`endTime`='{$_POST['endTime']}',`recurrence`='{$_POST['recurrence']},`finalDate`='{$_POST['finalDate']}' WHERE `id` = '$id' "; $mysqli->query($sql2) or die($mysqli->error);

Currently with the select box as is, it only grabs one of the days selected by a user.

2 Answers 2

4

First, Your select should have name with square brackets:

<select name='days[]' id='days' size=4 multiple="multiple">

This way when a user select more values (options) and submit the form You will receive an array of values he selected.

Then You have to loop through this array and insert each record:

foreach($_POST['days'] as $k => $v) {
    // here do the INSERT query with value $v
}

But Your query under the select box is telling us that this is not the end of the story...

Sign up to request clarification or add additional context in comments.

3 Comments

I don't understand how to loop through the array to insert into MySQL.
if (isset($_POST['submitted'])) { foreach($_POST AS $key => $value) { $_POST[$key] = $mysqli->real_escape_string($value); } Cannot take an array so I know I have to do something before this.
@에이바 I edited my code. I cannot say how the INSERT have to look like cos I don't know what other data are You inserting and how...
1
foreach ($_POST['days'] AS $key => $value) {
  // INSERT ... where $value is one of the values
}

1 Comment

Could I implode $_POST['days'] then insert it like if (isset($_POST['submitted'])) { foreach($_POST AS $key => $value) { $_POST[$key] = $mysqli->real_escape_string($value); } ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.