Post using for each php

时间:2016-03-04 18:21:27

标签: php mysql

I have a form with more than 1 input, it has a button to duplicate the form, while saving the data i am using foreach but its not working, help anyone.

<?php

if (isset($_POST['submit_button'])) {
    // Connecting to database
    $conn = mysql_connect('localhost', 'root', '');
    if (!$conn || !@mysql_SELECT_db('finalyearproject')) {
        echo("Can NOT connect to server");
        exit;
    }

    // Values to insert
    $functionID = 0;
    $functionName = $_POST ["functionName"];
    $functionDependency = $_POST["functionDependency"];
    $projectID = $_SESSION["projectID"];


    // insertion to user_details table
    foreach ($functionID as $eachInput) {

        $sql = "INSERT INTO function (functionID, functionName,functionDependency,projectID) 
            VALUES ('$eachInput', '$functionName', '$functionDependency','$projectID')";
        if ($conn->query($sql) === TRUE) {

            header("Location: du.php?message=success");
        } else {
            echo "Error updating record: " . $conn->error;
        }
    }
}
?>

this is my html form, hope it would help you people to fine my error

     <form action="analystCreateFunction.php" method="post" id="sign-up_area" role="form">
          <div id="dynamicInput">
      <div class="box box-default">
        <div class="box-header with-border">
         <p>Create Function</p>

        </div><!-- /.box-header -->
        <div class="box-body">
          <div class="row">
            <div class="col-md-6">
            <div class="form-group">
                <label class="label_fn control-label" for="first_name">Function Name:</label>
                <input id="first_name" name="functionName[]" type="text" placeholder="" class="input_fn form-control" required="">
            </div>

            </div><!-- /.col -->
            <div class="col-md-6">
            <div class="form-group">
                <label class="label_fn control-label" for="first_name">Function Dependency:</label>
                <input id="first_name" name="functionDependency[]" type="text" placeholder="" class="input_fn form-control" required="">
            </div>

            </div><!-- /.col -->
          </div><!-- /.row -->
        </div><!-- /.box-body -->
      </div><!-- /.box -->
      </div>

 <input type="button" class="btn btn-primary" value="Add another Function" onClick="addInput('dynamicInput');">
      <button id="submit_button" name="submit_button" class="btn btn-primary">Submit</button>
      </form>

0 个答案:

没有答案