PHP Crud 更新选择显示重复
PHP Crud Updating selection shows duplicate
我有一个 PHP Crud 系统。创建新记录时的选项之一是带有两个选项的选择框。网络人和管理员。当我更新记录时,我看到了这个选项,但它重复了并且有另一个选项,
像这样。我看不到如何显示数据库中的记录和其他选项。这可能吗?
<?php
include "config.php";
include "header.php";
if(isset($_GET['u'])):
if(isset($_POST['bts'])):
$stmt = $mysqli->prepare("UPDATE passwordtable SET school=?,usertype=?, password=?, notes=? WHERE id_personal=?");
$stmt->bind_param('sssss', $nm, $gd, $tl, $ar, $id);
$nm = $_POST['nm'];
$gd = $_POST['gd'];
$tl = $_POST['tl'];
$ar = $_POST['ar'];
$id = $_POST['id'];
if($stmt->execute()):
echo "<script>location.href='index.php'</script>";
else:
echo "<script>alert('".$stmt->error."')</script>";
endif;
endif;
$res = $mysqli->query("SELECT * FROM passwordtable WHERE id_personal=".$_GET['u']);
$row = $res->fetch_assoc();
?>
选择框代码为us
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<option><?php echo $row['usertype'] ?></option>
<option>Administrator</option>
<option>NetMan</option>
</select>
如果还是找不到答案请看下面的代码
<?php
$usertypes = array('Administrator' => 'Administrator', 'NetMan' => 'NetMan');
$selcted_usertype = !empty($row['usertype']) ? $row['usertype'] : "";
?>
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<?php
foreach ($usertypes as $user_type_val => $usertype_txt) {
$selected = ($user_type_val == $selcted_usertype) ? "selected='selected'" : "";
?>
<option <?php echo $selected; ?> value="<?php echo $user_type_val; ?>"><?php echo $usertype_txt; ?></option>
<?php }
?>
</select>
您需要为选项设置值属性。
我有一个 PHP Crud 系统。创建新记录时的选项之一是带有两个选项的选择框。网络人和管理员。当我更新记录时,我看到了这个选项,但它重复了并且有另一个选项,
像这样
<?php
include "config.php";
include "header.php";
if(isset($_GET['u'])):
if(isset($_POST['bts'])):
$stmt = $mysqli->prepare("UPDATE passwordtable SET school=?,usertype=?, password=?, notes=? WHERE id_personal=?");
$stmt->bind_param('sssss', $nm, $gd, $tl, $ar, $id);
$nm = $_POST['nm'];
$gd = $_POST['gd'];
$tl = $_POST['tl'];
$ar = $_POST['ar'];
$id = $_POST['id'];
if($stmt->execute()):
echo "<script>location.href='index.php'</script>";
else:
echo "<script>alert('".$stmt->error."')</script>";
endif;
endif;
$res = $mysqli->query("SELECT * FROM passwordtable WHERE id_personal=".$_GET['u']);
$row = $res->fetch_assoc();
?>
选择框代码为us
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<option><?php echo $row['usertype'] ?></option>
<option>Administrator</option>
<option>NetMan</option>
</select>
如果还是找不到答案请看下面的代码
<?php
$usertypes = array('Administrator' => 'Administrator', 'NetMan' => 'NetMan');
$selcted_usertype = !empty($row['usertype']) ? $row['usertype'] : "";
?>
<label for="gd">User Type</label>
<select class="form-control" id="gd" name="gd">
<?php
foreach ($usertypes as $user_type_val => $usertype_txt) {
$selected = ($user_type_val == $selcted_usertype) ? "selected='selected'" : "";
?>
<option <?php echo $selected; ?> value="<?php echo $user_type_val; ?>"><?php echo $usertype_txt; ?></option>
<?php }
?>
</select>
您需要为选项设置值属性。