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?

2
  • 2
    I'm missing the question. Commented Oct 15, 2015 at 8:18
  • How can i insert dynamic data to mysql? Commented Oct 15, 2015 at 8:19

3 Answers 3

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')");
}
Sign up to request clarification or add additional context in comments.

3 Comments

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
Single value input correctly. But project not inputed
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')");
    }

2 Comments

what about for project part?
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'";
}
?>

Comments

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.