1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Adding multiple text fields at once into mysql?

Discussion in 'PHP' started by baris22, May 17, 2011.

  1. #1
    hello all,



    this works perfect but is this the proper way to do this?


    
    
    <?
    /////////////////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" >
    
    
    PHP:

     
    baris22, May 17, 2011 IP
  2. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <?php
    
    $newhour_start = $_POST['newhour_start'];
    $newhour_stop = $_POST['newhour_stop'];
    $newhour_day = $_POST['newhour_day'];
    
    $count = min(count($newhour_start), count($newhour_stop), count($newhour_day));
    
    for($i=0;$i<$count;$i++)
    {
    if(!empty($newhour_start[$i]) && !empty($newhour_stop[$i]) && !empty($newhour_day[$i])
    {
    if(!mysql_query(sprintf("INSERT INTO `hour` (`hour_start`,`hour_end`,`hour_day`) VALUES (%d, %d, %d)", $newhour_start[$i], $newhour_stop[$i], $newhour_day[$i])))
    {
    // An error occurred.
    echo 'Error!';
    }
    }
    }
    
    PHP:
    That is how I'd personally do the PHP, or else something very similar to that.
     
    JoelLarson, May 17, 2011 IP