我正在尝试将动态表单字段值插入数据库。要在数据库中插入这些值,我试图在MySQL查询中应用foreach循环 代码如下:
if (isset($_POST['submit_val'])) {
if ($_POST['elements'],$_POST['quanity']){
foreach($_POST['elements'] as $elements){
foreach($_POST['quantity'] as $quantity){
$sql[] = "INSERT INTO create_campaign (elements, quantity) VALUES ('{$elements}','{$quantity}')";
}
}
foreach($sql as $query){
mysql_query($query);
}
}
}
<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[]" multiple>
</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()" id="add_field">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_val">
它给出错误并且没有发生数据库的插入,我该怎么办?
答案 0 :(得分:0)
尝试按如下方式使用..如果被此卡住,你可以得到完美的错误......
<?php
if (isset($_POST['submit_val'])) {
if ($_POST['elements'] && $_POST['quantity']){
foreach($_POST['elements'] as $elements){
foreach($_POST['quantity'] as $quantity){
echo $sql = "INSERT INTO create_campaign (elements, quantity) VALUES ('{$elements}','{$quantity}')";
//mysql_query($sql) or die(mysql_error);
}
}
}
}
?>
<form method="POST">
<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[]" multiple>
</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()" id="add_field">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_val">
</form>