gpt4 book ai didi

php - 通过php在表中动态插入行

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

我有 3 行。这是动态创建的。如果我们点击添加更多,那么这三个字段都是动态创建的。工作正常。

但是我想通过mysql查询动态地将这些字段插入到表中。我尝试过,但没有成功。请帮助任何人解决它。谢谢。

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
var rowCount = 1;

window.addMoreRows = function(frm) {
rowCount++;
var recRow = '<div id="rowCount' + rowCount + '"><div class="col-sm-3 text-left">College Name:</div><div class = "col-sm-9"><input type="text" name = "collegename[]" placeholder = "College Name" class = "form-control"/></div><div class = "col-sm-3 text-left" > Affiliated University: </div><div class = "col-sm-9" ><input type = "text" name = "univ[]" placeholder = "Affiliated University" class = "form-control" / > </div><div class = "col-sm-3 text-left" > Course Name: </div> <div class = "col-sm-9" ><input type = "text" name = "cname[]" placeholder = "Course Name" class = "form-control"/></div><div class = "col-sm-3 text-left" > Major & Subject: </div><div class = "col-sm-9"><input type = "text" name = "msub[]" placeholder = "Major Subject" class = "form-control"/></div><div class = "col-sm-3 text-left"> Final & Percentage: </div><div class = "col-sm-9"><input type = "text" name = "fper[]" placeholder = "Final Percentage" class = "form-control"/></div><a href = "javascript:void(0);" onclick = "removeRow (' + rowCount + ');" >Delete</a></div> ';
$('#addedRows').append(recRow);
}
window.removeRow = function(removeNum) {
$('#rowCount'+removeNum).remove();
}
</script>
<?php
extract($_POST);
if (isset($_POST['submit_val'])) {
if ($_POST['dynfields']) {
foreach (array_keys($dynfields) as $key) {
$txt = $dynfields[$key];
$txt1 = $univ[$key];
$txt2 = $cname[$key];
$txt3 = $msub[$key];
$txt4 = $fper[$key];
$query = mysql_query("INSERT INTO dynamicfield VALUES ('','','$txt','$txt1','$txt2','$txt3','$txt4')");

}
}
echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>";
mysql_close();
}
?>
<form action="" method="post" id="addedRows">
<h5 align="left"><u style="color:#449D44;">Other Studies</u>-<span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">Add More Other Studies</span></h5>
<span id="rowId">
<div class="col-sm-3 text-left">College&nbsp;Name: </div>
<div class="col-sm-9"><input type="text" name="dynfields[]" placeholder="College Name" class="form-control" /></div>

<div class="col-sm-3 text-left">Affiliated&nbsp;University: </div>
<div class="col-sm-9"><input type="text" name="univ[]" placeholder="Affiliated University" class="form-control" /></div>
<div class="col-sm-3 text-left">Course&nbsp;Name: </div>
<div class="col-sm-9"><input type="text" name="cname[]" placeholder="Course Name" class="form-control" /></div>
<div class="col-sm-3 text-left">Major&nbsp;Subject: </div>
<div class="col-sm-9"><input type="text" name="msub[]" placeholder="Major Subject" class="form-control" /></div>
<div class="col-sm-3 text-left">Final&nbsp;Percentage: </div>
<div class="col-sm-9"><input type="text" name="fper[]" placeholder="Final Percentage" class="form-control" /></div>
</span>
<input type="submit" name="submit_val" value="Submit" />

最佳答案

我找不到您命名输入字段的位置。

根据您想要从 $_POST 参数检索的数组命名您提交的输入字段。

<input name="dynfields[]"/>

括号告诉 PHP 提交的值是数组的一部分。在您编辑的问题中,HTML 将如下所示。

<tr>
<td><input name="dynfields[]" type="text" value="" size="17%"/></td>
<td><input name="dynfields1[]" type="text" value="" /></td>
<td><input name="dynfields2[]" type="text" value="" /></td>
</tr>
<tr>
<td><input name="dynfields[]" type="text" value="" size="17%"/></td>
<td><input name="dynfields1[]" type="text" value="" /></td>
<td><input name="dynfields2[]" type="text" value="" /></td>
</tr>
(...)

提交表单后,您应该能够访问 3 个单独的数组:

$_POST['dynfields'];
$_POST['dynfields1'];
$_POST['dynfields2'];

关于php - 通过php在表中动态插入行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38036956/

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