$sql = mysqli_query($conn, "INSERT INTO april(time,level,room) VALUES ('".$_POST['time'][$i]."','".$_POST['level'][$i]."','".$_POST['room'][$i]."')";
$conn is connection string which is just example which I just told you $_POST['time']
is this field multiple or notif(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
<form action="t3096.php" method="post" >
<table>
<tr>
<th>Time</th>
<th>Level</th>
<th>Room</th>
</tr>
<tr>
<td>
<select id="drop" name="time">
<option value = "8:30-12:30">8:30-12:30</option>
<option value = "12:45-1:45">12:45-1:45</option>
<option value = "2:00-6:00">2:00-6:00</option>
</select>
<td> <input type="text" name="level[]" id="level_1">
<td><input type="text" name="room[]" id="Room_1">
</tr>
<tr>
<td>
<td> <input type="text" name="level[]" id="level_2">
<td><input type="text" name="room[]" id="Room_2">
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit form"></td>
</tr>
</table>
</form>
Here is the PHP<?php
// Simulate db connection
$proc = true;
require('connection.php');
$link = $conn;
// Get our POST variables cleanly
$time = isset($_POST['time']) ? $_POST['time'] : false;
$level = isset($_POST['level']) ? $_POST['level'] : false;
$room = isset($_POST['room']) ? $_POST['room'] : false;
// Check for valid input
// level and room both arrays and have the same number of elements
if ($time && is_array($level) && is_array($room) && count($level) == count($room)) {
// either clean or validate time
$time = preg_replace('/[^\d-:]/','', $time);
// We use a batch insert to insert our values. One query with multiple values
// this is more efficient than running an individual query for each insert
$query = <<< QUERY
INSERT INTO april(time,level,room) VALUES
QUERY;
foreach($level as $i => $l) {
$lvl = mysqli_real_escape_string($link, $[url="http://www.marcorpsa.com/ee/t3096.html"][/url]l);
$rm = mysqli_real_escape_string($link, $room[$i]);
$query .= <<< VALUES
('{$time}','{$lvl}','{$rm}'),
VALUES;
}
$query = trim($query, ',');
mysqli_query($link, $query);
}
else {
// handle invalid input here
echo "Invlalid inputs found";
}
You can see a working sample here $time = preg_replace('/[^\d-:]/','', $time);
I would use the non-indexed version as it is easier to extend without remembering to update the indices.
<?php
$myArray = array();
$myArray[0] = "hello";
$myArray[1] = "world";
<?php
$myArray = array();
$myArray[] = "hello";
$myArray[] = "world";
<?php
$myArray = array();
$myArray[10] = "hello";
$myArray[20] = "world";
<input type="text" name="name[]">
<input type="text" name="name[]">
...will be like writing this code:$_REQUEST = array();
$_REQUEST["name"] = array();
$_REQUEST["name"][] = "Value of the first name input";
$_REQUEST["name"][] = "Value of the second name input";