gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 18:29:53 24 4
gpt4 key购买 nike

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

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>

最佳答案

您应该声明一个变量,例如 x=1 并在输入数组中使用它,例如 name="input_name[+ x +]"并在每个输入字段后递增变量,即 x++您可以在 php Controller 中使用 inputName[1]、input_name[2] 访问这些输入

关于php - 如何使用php在数据库中插入动态添加的表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45781536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com