Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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']
    ));
share|improve this question
1  
You could prepare the statement once and execute with different value-arrays in a loop. –  feeela Jun 24 '14 at 17:21

1 Answer 1

up vote 1 down vote accepted

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);
share|improve this answer
    
thanks. i'll give it a try –  Azukah Jun 24 '14 at 19:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.