0

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!

7
  • 1
    What's the database structure? Commented Mar 25, 2012 at 7:05
  • please add more to understand Commented Mar 25, 2012 at 7:08
  • Voting to close as too localized. You've got two questions going on here: one about duplicate GET or POST variable names, and one about inserting into MySQL. Commented Mar 25, 2012 at 7:12
  • To expand on Michael and Mayank's comments, sample code is king. If you have a question that concerns databases, include appropriate SQL statements, such as the table schema (as CREATE TABLE statements) and (if appropriate) sample data (as INSERT ... VALUES statements). See Writing the Perfect Question for more. Commented Mar 25, 2012 at 7:26
  • @DavidWolever, What do you think about my answer? Should be enough for such type of question, dont you think? Commented Mar 25, 2012 at 7:29

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

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

You can add them separated by commas (or any other delimiter provided it does not create SQL injection). Like

<?php
$months = $_REQUEST['month'];
$sql = "INSERT INTO `YOURTABLE` values ('$id','$name','";
$comma_flag = false;
foreach ($months as $month)
{
    $sql = $sql.($comma_flag?",":"").$month;
    $comma_flag = true;
}
$sql .="');";
$dbc = mysql('localhost','username','password');
mysql_select_db('your_database',$dbc);
mysql_query($sql,$dbc);
?>

Note: The MySQL Column month_name must be varchar of larger size.

Later if you want to retreive, after 'SELECT FROM ... ' you can split it using

$months = explode($result,",");

1 Comment

The sample code is vulnerable to SQL injection, which is a very serious security risk. Moreover, the mysql extension is outdated and on its way to deprecation. New code should use mysqli or PDO, both of which have important advantages, such as support for prepared statements. Prepared statement parameters aren't vulnerable to injection.

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.