Azukah
Azukah

Reputation: 227

PHP PDO insert multiple rows/values into mySQL table

I'm submitting this form into a table in mySQL db. the field max is the same for all rows but each row has 2 unique values.

<form action="file.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> 
    <label>Max: </label><input type="number" name="max" value="" autocomplete="off">

    <label>Times (hh:mm): </label>
    <input type="time" name="time1_1" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_1" value="" autocomplete="off">
    <input type="time" name="time1_2" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_2" value="" autocomplete="off">
    <input type="time" name="time1_3" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_3" value="" autocomplete="off">
    <input type="time" name="time1_4" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_4" value="" autocomplete="off">
    <input type="time" name="time1_5" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_5" value="" autocomplete="off">
    <input type="time" name="time1_6" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_6" value="" autocomplete="off">
    <input type="time" name="time1_7" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_7" value="" autocomplete="off">
    <input type="time" name="time1_8" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_8" value="" autocomplete="off">
    <input type="time" name="time1_9" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_9" value="" autocomplete="off">
    <input type="time" name="time1_10" value="" autocomplete="off"><span> &amp; </span><input type="time" name="time2_10" value="" autocomplete="off">
    <button name="submit" type="submit">Submit</button>
</form> 

the table has all time values and max quantity from the form.

    ------------------------------------
   |    id      max   time1    time2    |
   |    1       25    13:30    19:30    |
   |    2       25    14:00    20:00    | 
    ------------------------------------

Is there a way to submit all 20 time values (if not empty) instead of doing the following for each?

    $sql = "INSERT INTO tbl_name (max, time1, time2) VALUES (:max, :time1, :time2)";
    $stmt = $db->prepare($sql);
    $stmt->execute(array(
    ":max" => $_POST['max'],
    ":time1" => $_POST['time1_1'],
    ":time2" => $_POST['time2_1']
    ));

Upvotes: 0

Views: 718

Answers (1)

Fabricator
Fabricator

Reputation: 12772

Just bind all values.

$sql = "INSERT INTO tbl_name (max, time1, time2) VALUES (?,?,?)".str_repeat(',(?,?,?)', 9);
$stmt = $db->prepare($sql);
$params = array();
for ($i = 1; $i <= 10; $i++) {
    $params[] = $_POST['max'];
    $params[] = $_POST["time1_$i"];
    $params[] = $_POST["time2_$i"];
}
$stmt->execute($params);

Upvotes: 1

Related Questions