I have a HTML form with a with multiple options, in which is possible to select multiple values (options).
I was using the implode, to INSERT the values in a MySQL table separated by a "," but now i need to change it. I've searched everywhere, but i don't seem to find any solution for my problem.
What i need to do, is to create different rows on form submit, with different values from the selected options.
I was using this previous code to insert into the table, the values separated by a "," .
$event = mysqli_real_escape_string($link, $_REQUEST['event']);
$date = mysqli_real_escape_string($link, $_REQUEST['date']);
$local = mysqli_real_escape_string($link, $_REQUEST['local']);
$disc = mysqli_real_escape_string($link, $_REQUEST['disc']);
$username_string = implode(', ', $_POST['username']);
$sql = "INSERT INTO events (event, date, local, disc, username) VALUES ('$event','".date('d-m-Y', strtotime($date))."', '$local', '$disc', '$username_string')";
And this is the HTML form i'm using.
<form action="insert/insertEvents.php" id="newEvent" method="post">
<div class="form-group">
<label for="user">Utilizador</label>
<br>
<select class="selectpicker" name="username[]" multiple>
<?php
while ($row = mysqli_fetch_array($query2))
{ echo '
<option>'.$row['username'].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="nameEvent">Evento</label>
<br>
<input type="text" name="event" class="form-control" id="event">
</div>
<div>
<label for="reportDate">Data</label>
<br>
<div class="bfh-datepicker">
<div class="input-prepend bfh-datepicker-toggle form-group" data-toggle="bfh-datepicker">
<span class="add-on"><i class="icon-calendar"></i></span>
<input type="text" name="date" id="date" class="form-control">
</div>
<div class="bfh-datepicker-calendar">
<table class="calendar table table-bordered">
<thead>
<tr class="months-header">
<th class="month" colspan="4">
<a class="previous" href="#"><i class="icon-chevron-left"></i></a>
<span></span>
<a class="next" href="#"><i class="icon-chevron-right"></i></a>
</th>
<th class="year" colspan="3">
<a class="previous" href="#"><i class="icon-chevron-left"></i></a>
<span></span>
<a class="next" href="#"><i class="icon-chevron-right"></i></a>
</th>
</tr>
<tr class="days-header"></tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
</div>
<div class="form-group">
<label for="namefat">Local</label>
<br>
<input type="text" name="local" class="form-control" id="local" placeholder="ex: Porto">
</div>
<div class="form-group">
<label for="namefat">Descrição</label>
<br>
<textarea rows="12" name="disc" class="form-control" id="disc" form="newEvent"></textarea>
</div>
<button type="submit" class="btn btn-primary">Adicionar</button>
</form>
Can someone please explain me how should i solve this? I searched different questions from other users, and i couldn't find a question which i could identify myself with.
Do you want to insert all username?? Something like this?
//first remove your implode code
foreach($_POST['username'] as $username){
$sql = "INSERT INTO events (event, date, local, disc, username) VALUES ('$event','".date('d-m-Y', strtotime($date))."', '$local', '$disc', '$username')";
//call the insert query
}