如何使用php在数据库中插入动态添加的表单字段

时间:2017-08-20 11:15:55

标签: php mysql angularjs database forms

我想使用PHP在数据库中添加动态表单字段。我使用angular来添加动态表单字段。问题是,当我尝试将此数据插入数据库时​​,只有最后一个表单字段插入数据库中。所以,我使用数组和循环来递增并将此表单字段更新到数据库中。但不知怎的,查询工作不正常,数据也没有插入数据库。谁能告诉我这里有什么问题?我被卡住了。请帮忙。 Thanx提前

Php Code:
<?php
if(isset($_POST['submit_row']))
{
$link = mysqli_connect("localhost", "root", "", "midata");

// Check connection
if($link === false){
    die("ERROR: Could not connect. " . mysqli_connect_error());
}

 $camp_name = mysqli_real_escape_string($link, $_REQUEST['camp_name']);
 $start_date = mysqli_real_escape_string($link, $_REQUEST['start_date']);
 $end_date = mysqli_real_escape_string($link, $_REQUEST['end_date']);


$store = mysqli_real_escape_string($link, $_REQUEST['$store']);
$elements= $mysqli->real_escape_string($_POST['elements']);
$quantity = $mysqli->real_escape_string($_POST['quantity']);

$description = mysqli_real_escape_string($link, $_REQUEST['description']);

for($i=0;$i<count($elements);$i++)
 {
  if( $elements[$i]!="" && $quantity[$i]!="")
  {

$sql = "INSERT INTO create_campaign(camp_name, start_date, end_date,store,elements,quantity, description )
VALUES('$camp_name',' $start_date', '$end_date','$store','$elements[$i]', '$quantity[$i]', '$description')";

if(mysqli_query($link, $sql)){
    echo "Records added successfully.";
} else{
    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_query($sql, $link);
}
}
}


?>


HTML Code:
  <div class="row col-md-12" ng-app="angularjs-starter" ng-controller="MainCtrl">
                                                    <fieldset  data-ng-repeat="choice in choices" name="records">
                                                        <label for="inputPassword3" class="col-md-1 control-label">Elements</label>
                                                        <div class="form-group col-md-3 ">
                                                            <input type="text" placeholder="Campaign Name" ng-model="choice.name" class="form-control c-square c-theme input-lg" name="elements[]"> 
                                                        </div>
                                                        <label for="inputPassword3" class="col-md-1 control-label">Quantity</label>
                                                        <div class="form-group col-md-3" >
                                                           <select class="form-control  c-square c-border-2px c-theme"  name="quantity[]>
                                                             <option value="1">100</option>
                                                             <option value="2">200</option>
                                                             <option value="3">300</option>
                                                             <option value="4">400</option>

                                                            </select>
                                                        </div>

                                                     <button type="button" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square" ng-click="addNewChoice()" >add</button>
                                                     <button   ng-show="$last" ng-click="removeChoice()" class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square" >Remove</button>
                                                     </fieldset>  
                                                </div>   
                                           </div>
                                        </div>
                                      <div class="form-group">
                                            <input type="text" placeholder="Description" class="form-control c-square c-theme input-lg" name="description">
                                      </div>
                                        <input class="btn c-theme-btn c-btn-uppercase btn-lg c-btn-bold c-btn-square" value="Submit" type="submit" name="submit_row">

                                    </form>
                                </div>

1 个答案:

答案 0 :(得分:0)

您应该声明一个类似x = 1的变量,并在输入数组中使用它,例如name =“ input_name [+ x +]” 并在每个输入字段后增加变量,即x ++ 您可以在PHP控制器中使用inputName [1],input_name [2]访问这些输入