我在mysql数据库中有近7000个数据。有些列是空的,我想用相关数据填充它们。我正在显示mysql中的数据,并用表单和插入的输入包装空字段。代码如下;
while ($row = mysqli_fetch_array($results)) {
echo '
<form action="" method="post">
<tr>
<td>'._date($row["time"]).'</td>
<td>$row["name"]</a>
<br><input name="name" class="form-control input-sm" value="New name" >
</td>
<td>info@domain.com<br>
<input name="email" class="form-control input-sm" value="newemail@domain.com" >
</td>
<td>0123456789<br>
<input name="telephone" class="form-control input-sm" value="0987654321" >
</td>
<td>111 Some street name<br>
<input name="address" class="form-control input-sm" value="New road name and address" >
</td>
<td>category</td>
<td>
<button type="submit" class="btn btn-success btn-flat"><i class="fa fa-save fa-fw"></i></button>
<a href="index.php?p=subscribers&delete='.$row["id"].'" class="btn btn-danger btn-flat"><i class="fa fa-trash fa-fw"></i></a>
</td>
</tr>
</form>
';}
如您所见,我可以选择单独保存它们,但我也想一次保存所有显示的数据。显示所有数据可能非常慢,所以我想我需要一次显示一小部分。
有人可以告诉我,我可以一次保存所有显示的表单输入吗?
答案 0 :(得分:0)
经过更多的研究,我终于设法找到了解决方案,我在这里发帖给任何需要它的人。
if (isset($_POST['update'])){
for($i=0; $i<count($_POST['id']); $i++){
$sql = "
UPDATE test
SET
name = '".$_POST['name'][$i]."',
email = '".$_POST['email'][$i]."',
telephone = '".$_POST['telephone'][$i]."'
WHERE id = '".$_POST['id'][$i]."'";
if ($con->query($sql) === TRUE) {
//echo "Record updated successfully";
} else {
echo "Error updating record: " . $con->error;
}
}
}
$sql = "SELECT * FROM test ORDER BY id ASC LIMIT 5";
$results = $con->query($sql);
if (mysqli_num_rows($results) > 0) {
echo '<form action="" method="post">';
echo '<input type="hidden" name="update" >';
// displaying records.
while ($row = mysqli_fetch_array($results)) {
echo '
<input type="hidden" name="id[]" value="'.$row["id"].'" >
<tr>
<td><input type="text" name="name[]" class="form-control input-sm" value="'.$row["name"].'" ></td>
<td><input type="text" name="email[]" class="form-control input-sm" value="'.$row["email"].'" ></td>
<td><input type="text" name="telephone[]" class="form-control input-sm" value="'.$row["telephone"].'" ></td>
</tr>
';
}
echo '<input type="submit" value="update">';
echo '</form>';
}else {
echo "No records are found.";
}