这是将多个文本字段一次添加到mysql中的正确方法吗?

时间:2011-05-17 09:42:15

标签: php mysql

这种方法很完美,但这是正确的方法吗?

<?
/////////////////add/////////////////////////////////

    for ($i=0; $i<count($_POST['newhour_start']); $i++) {   
        if ($_POST['newhour_start'][$i] !="" && $_POST['newhour_end'][$i] !="" ) {
            $inserthour = mysql_query("INSERT INTO hour 
            (hour_start,hour_end,hour_day) VALUES 
            ('".$_POST['newhour_start'][$i]."','".$_POST['newhour_end'][$i]."','".$_POST['newhour_day'][$i]."')");
        }
    }
?>
Start:<input name="newhour_start[]" type="text" id="newhour_start" > End: <input name="newhour_end[]" type="text" id="newhour_end" ><input name="newhour_day[]" type="hidden" id="newhour_day" value="Monday" >

Start:<input name="newhour_start[]" type="text" id="newhour_start" > End: <input name="newhour_end[]" type="text" id="newhour_end" ><input name="newhour_day[]" type="hidden" id="newhour_day" value="Monday" >

Start:<input name="newhour_start[]" type="text" id="newhour_start" > End: <input name="newhour_end[]" type="text" id="newhour_end" ><input name="newhour_day[]" type="hidden" id="newhour_day" value="Monday" >

1 个答案:

答案 0 :(得分:3)

您可以一次插入多行:

mysql_query("INSERT INTO hour 
            (hour_start,hour_end,hour_day) VALUES 
            (1, 2, 3),
            (4, 5, 6)
);