0

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']
    ));
1
  • 1
    You could prepare the statement once and execute with different value-arrays in a loop. Commented Jun 24, 2014 at 17:21

1 Answer 1

1

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);
Sign up to request clarification or add additional context in comments.

Comments

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.