每个申请人都有自己的代码,该代码发生在“ applicant_code”中,并且可能有1个或多个受抚养人。该表中的主键是'dep_id',它会自动递增。我想更改一行中一个字段的值,但显然它只会更新最后一行。
第一行不更新,仅第二行。我这个问题已经困扰了近一个星期了。请帮助我:(我认为查询只执行一次。
<?php
include 'php/connection.php';
if (isset($_POST['edit'])) {
$txtDependentID = $_POST["txtDependentID"];
$txtDependentName = $_POST["txtDependentName"];
$txtDependentRelation = $_POST["txtDependentRelation"];
$txtDependentBirthDate = $_POST["txtDependentBirthDate"];
$numdep = count($txtDependentName);
for($i=0;$i<$numdep;$i++) {
$sql = "UPDATE dependent_info SET dep_name='".$txtDependentName[$i]."', dep_relation='".$txtDependentRelation[$i]."', dep_birthdate='".$txtDependentBirthDate[$i]."' WHERE applicant_code=
'".$_GET['id']."';";
}
if (mysqli_query($conn, $sql)) {
echo "<script type='text/javascript'>
alert('Success!');
window.location.replace('http://localhost/1/view.php');
</script>";
} else {
echo "Error ".mysqli_error($conn);
}
}
这是我的HTML文件,用于在文本字段内显示数据库中的值。
<table class="table" id="dependent_dynamic_field">
<tr>
<th style="width: 500px">ID</th>
<th style="width: 500px">Dependent Name</th>
<th style="width: 400px">Relation</th>
<th style="width: 305px">Birthdate</th>
<th style="width: 60px"></th>
</tr>
<?php
$dep_name = '';
$dep_relation = '';
$dep_birthdate = '';
$sql = "SELECT * FROM dependent_info;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result))
{
$dep_id = $row['dep_id'];
$dep_name = $row['dep_name'];
$dep_relation = $row['dep_relation'];
$dep_birthdate = $row['dep_birthdate'];
?>
<tr>
<td style="width: 500px"><input type="text" name="txtDependentID" value="<?=$dep_id?>" class="form-control name_list" /><span style="color: red" id="dependentname"></span></td>
<td style="width: 500px"><input type="text" name="txtDependentName[]" id="txtDependentName" value="<?=$dep_name?>" class="form-control name_list" /><span style="color: red" id="dependentname"></span></td>
<td style="width: 400px"><input type="text" name="txtDependentRelation[]" id="txtDependentRelation" value="<?=$dep_relation?>" class="form-control name_list" /><span style="color: red" id="dependentrelation"></span></td>
<td style="width: 305px"><input type="date" name="txtDependentBirthDate[]" id="txtDependentBirthDate" value="<?=$dep_birthdate?>" class="form-control name_list" /><span style="color: red" id="dependentbirthdate"></span></td>
<td style="width: 60px"><button type="button" name="add_more_dependent" disabled id="add_more_dependent" class="btn btn-warning"><span style="font-size: 14px;" class="glyphicon glyphicon-plus" ></span></button></td>
</tr>
<?php
}
}
?>
</tr>
</table>
这是我将它们插入数据库的方式。正常运行
$number = count($_POST["txtDependentName"]);
if($number > 0){
for($i=0; $i<$number; $i++){
$sql .= "INSERT INTO dependent_info (dep_name, dep_relation, dep_birthdate) VALUES(mysqli_real_escape_string($conn, $_POST["txtDependentName"][$i])."','".mysqli_real_escape_string($conn, $_POST["txtDependentRelation"][$i])."','".mysqli_real_escape_string($conn, $_POST["txtDependentBirthDate"][$i])."');";
}
}