March 25, 2012

php add inputs with same name to mysql

Question by gogu

I have a form with some generated input fields like theese:

 <input value="March" name="month[March]" type="hidden"> 
<input value="April" name="month[April]" type="hidden"> 
<input value="May" name="month[May]" type="hidden">

I need to add the data from each input to mysql, but i don`t know how if they have name[monthname].The table structure is id, name and month_name. So i need to add the input data to month_name column. Thanks!

Answer by Starx

You can iterate over different input with same name in this way

foreach($_POST['month'] as $value) {
   echo $value;
   //Use the value in the query, it gets April March may, one by one
}

P.S: Assuming you are using POST method to submit the form.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!