修改:跟添加相似,需要显示默认值
先嵌入php代码 查询数据库
$code = $_GET["c"]; $db = new MySQLi("localhost","root","159357","renyuanxinxi"); $ainfo = "select * from info where code='{$code}'"; $binfo = $db->query($ainfo); $cinfo = $binfo->fetch_row();
在网页中显示所查询的值(在表单中嵌入相应的简短php代码,进行赋值设定)
<form action="xiugaichuli.php" method="post"> <div><input type="hidden" name="code" value="<?php echo $ainfo[0]; ?>" /></div> <div>姓名:<input type="text" name="name" value="<?php echo $ainfo[1]; ?>" /></div> <div>性别: //true false判定 <input type="radio" name="sex" value="1" <?php echo $ainfo[2]?"checked='checked'":"" ?> />男 <input type="radio" name="sex" value="0" <?php echo $ainfo[2]?"":"checked='checked'" ?> />女 </div> <div>民族: <select name="nation"> <?php $sql = "select * from nation"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) //下拉列表设默认 { if($v[0]==$ainfo[3]) { echo "<option selected='selected' value='{$v[0]}'>{$v[1]}</option>"; } else { echo "<option value='{$v[0]}'>{$v[1]}</option>"; } } ?> </select> </div> <div>生日:<input type="text" name="birthday" value="<?php echo $ainfo[4]; ?>" /></div> <div><input type="submit" value="修改" /></div> </form>
建立修改处理页面(先取值、再用修改语句 根据主键值 选择所要修改的列、然后再执行)
<?php $code = $_POST["code"]; //取值 $name = $_POST["name"]; $sex = $_POST["sex"]; $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new MySQLi("localhost","root","159357","renyuanxinxi"); $sql = "update info set name='{$name}',sex={$sex},nation='{$nation}',birthday='{$birthday}' where code='{$code}'";//用修改语句 根据主键值 选择所要修改的列 $db->query($sql); //执行 header("location:main.php");