我有问题,因为我使用循环“ while”来加载有关ID Domains和domains_name的数据库信息。接下来,我要输入复选框,因为我想在设置时删除域复选框。我确实循环执行“ foreach”,但是此代码仅能处理一条记录。如何更改代码以处理更多记录
<?php
$result = $connect->query("SELECT * FROM domains where id_person='$id_person'");
$how_nick = $result->num_rows;
if ($how_nick != 0) {
while($data = $result->fetch_assoc()) {
?>
<tr class="staff">
<td class="staff">
Nazwa Domeny:<br>
<input class="staff" type="text" value="<?php echo $data['domain_name']; ?>" name="domain_name[]" placeholder="<?php echo $data['domain_name']; ?>" onfocus="this.placeholder='<?php echo $data['domain_name']; ?>'" onblur="this.placeholder='<?php echo $data['domain_name']; ?>'"/><br />
</td>
<td class="staff">Usuń:<br>
<input type="checkbox" name="id[]" value="<?php echo $data['id'];?>"/>
</td>
</tr>
<?php
}}}}
?>
更多代码循环“ foreach”
if(isset($_POST['send'])) {
$all_ok=true;
$id = $_POST['id'];
$domain_name = $_POST['domain_name'];
foreach ($domain_name as $key => $value)
{
if((preg_match('/[ćęłńóśźżA\/]/',$value)) || (!preg_match('/[.]/',$value)))
{
$all_ok=false;
$score="Formuła domeny".' <b> '.$value.' </b> '. "jest nie poprawna!".'<br>';
}else{
$result = $connect->query("SELECT id FROM domains WHERE domain_name='$value' AND id_person != '$id_person'");
if (!$result) throw new Exception($connect->error);
$how_domain_name = $result->num_rows;
if($how_domain_name>0) {
$all_ok=false;
$score="Istnieje już domena".' <b> '.$value.' </b> '.'<br>';
}
}
foreach ($id as $key => $value) {
if(isset($value)){
$score=$value;
}
}
if ($all_ok==true) {
$count = count($id);
for($i=0;$i<$count;$i++) {
$connect->query('UPDATE domains
SET domain_name="'.$domain_name[$i].'"
WHERE id='.(int)$id[$i].'');
}
if(!empty($delete_domain)){
$connect->query('DELETE FROM domains WHERE id='.$value.'');
}
$_SESSION['well_done']=true;
header('Location: change_data_confirm.php');
}
}
答案 0 :(得分:0)
一个原因是您在内部循环中覆盖了$ key,$ value变量-
foreach ($id as **$key => $value**) {
if(isset($value)){
$score=$value;
}
}
我认为这是问题所在。