0

Here is my html part

<form method="post" action="collect_vals.php">
    <div id="input_fields">
        <div><input type="text" name="name[]"> <input type="text" name="project[]"> <span class="fa fa-plus-circle" id="add_field"></span></div>
    </div>
    <input type="submit" value="submit">
</form>    

Details on: jsFiddle

I follow this link , it can save single field data. But how to insert multiple data to mysql.

How can i insert dynamic data to mysql?

Community
  • 1
  • 1
Russell
  • 1,624
  • 5
  • 23
  • 43

3 Answers3

1

Access project names using the key, like this

foreach($_POST['name'] as $key => $val)
{
    $proj = $_POST['project'][$key];
    $insert = mysql_query("INSERT INTO table_name (column1,colunm2) values ('$val','$proj')");
}
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
  • My code: `if(isset($_POST["name"]) && isset($_POST["project"])){ foreach($_POST["name"] as $key => $text_field){ $proj = $_POST['project'][$key]; if ($all_names == '') { $all_names .= "(".$proj.",'".mysql_real_escape_string($text_field) ."') "; } else { $all_names .= ",(".$proj.", '".mysql_real_escape_string($text_field) ."') "; } } } $sql = "INSERT INTO student (id, name ) VALUES $all_names "; ` How to add your code – Russell Oct 15 '15 at 09:56
  • Single value input correctly. But project not inputed – Russell Oct 15 '15 at 09:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/92370/discussion-between-niranjan-n-raju-and-nikolas). – Niranjan N Raju Oct 15 '15 at 10:04
0

You can use foreach on PHP side. Your HTML part is good.

Here is an example:

foreach($_POST['name'] as $a)
    {
        $insert = mysql_query("INSERT INTO table_name (column_name) values ('$a')");
    }
Deniz B.
  • 2,532
  • 1
  • 18
  • 35
0
<form method="post" action="collect_vals.php">
    <div id="input_fields">
        <div>
        <input type="text" name="name[]"> 
        <input type="text" name="project[]"> 
        <span class="fa fa-plus-circle" id="add_field"></span>
    </div>
    </div>
    <input type="submit" value="submit">
</form>   

collect_vals.php

<?
extract($_POST);

$SizeOfName=sizeof($name);
for($i=0;$i<$SizeOfName;$i++)
{
    $Name=$name[$i];
    $Project=$project[$i];

    $QueryInsert="INSERT INTO TableName SET NameColumnName='$Name',ProjectColumnName='$Project'";
}
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77