數據庫中兩張表:php
info表中「民族」關聯了nation表中的「code」。mysql
php經過後臺編輯info表中民族顯示成用戶可看懂及可直接修改的選項。sql
新建xinxi.php文件顯示info中數據,而且民族選項顯示非代號。數據庫
<?php $de = new mysqli("localhost","root","","12345"); $e="select * from info"; $result = $de->query($e); $atter = $result->fetch_all(); echo "<table border=1 >"; echo "<tr> <td>代號</td> <td>姓名</td> <td>性別</td> <td>民族</td> <td>出生日期</td> <td>操做</td> </tr> "; foreach($atter as $v) { $sex = $v[2]; //性別用 男和女顯示。 $sex = $sex?"男":"女"; $nation = $v[3]; //民族用非代號顯示。 $sqln = "select name from nation where code='{$nation}'"; $r = $de->query($sqln); $atr = $r->fetch_row(); echo " <tr> <td>{$v[0]}</td><td>{$v[1]}</td><td>{$sex}</td><td>{$atr[0]}</td><td>{$v[4]}</td> <td> <a href='xiugai.php?code={$v[0]}'>修改</a> </td> </tr> "; } echo " </table>"; echo "<a href='tianjia.php'>添加數據</a>" ?>
顯示結果:post
添加數據:fetch
同路徑下新建tianjia.phpcode
<h1>添加數據</h1> <form action="tianjiachuli.php" method="post"> <input type="hidden" name="code" /> <br /> 姓名:<input type="text" name="name" /><br /> 性別:<input type="radio" name="sex" value="1" />男 <input type="radio" name="sex" value="0" />女 <br /> 民族: //使用下拉獲取民族選項的代號 <select name="nation"> <?php $db = new MySQLi("localhost","root","","12345"); $sql = "select * from nation"; $result = $db->query($sql); $attr = $result->fetch_all(); foreach($attr as $v) { echo " <option value='{$v[0]}' >{$v[1]}</option> "; } ?> </select> <br /> 生日:<input type="text" name="birthday" /><br /> <input type="submit" value="提交"/> </form>
同路徑下新建tianjiachuli.phporm
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new mysqli("localhost","root","","12345"); $sql = "insert into info values('{$code}','{$name}','{$sex}','{$nation}','{$birthday}')"; $r = $db->query($sql); if($r) { header("location:xinxi.php"); } else { echo "添加失敗"; } ?>
修改數據:blog
同路徑下新建xiugai.phpinput
<?php $code = $_GET["code"]; $db = new MySQLi("localhost","root","","12345"); $sql = "select * from info where code='{$code}'"; $result = $db->query($sql); $attr = $result->fetch_row(); ?> <h1>修改數據</h1> <form action="xiugaichuli.php" method="post"> <input type="hidden" name="code" value="<?php echo $attr[0] ?>" /> <br /> 姓名:<input type="text" name="name" value="<?php echo $attr[1] ?>" /><br /> 性別:<input type="radio" name="sex" value="1" <?php echo $attr[2]?"checked='checked'":""; ?> />男 <input type="radio" name="sex" value="0" <?php echo $attr[2]?"":"checked='checked'"; ?> />女 <br /> 民族: <select name="nation"> <?php $sqln = "select * from nation"; $result = $db->query($sqln); $atter = $result->fetch_all(); foreach($atter as $v) { if($attr[3]==$v[0]) { echo "<option selected='selected' value='{$v[0]}'>{$v[1]}</option>"; } else { echo " <option value='{$v[0]}' >{$v[1]}</option> "; } } ?> </select> <br /> 生日:<input type="text" name="birthday" value="<?php echo $attr[4] ?>" /><br /> <input type="submit" value="修改"/> </form>
同路徑下新建xiugaichuli.php
<?php $code = $_POST["code"]; $name = $_POST["name"]; $sex = $_POST["sex"]; $nation = $_POST["nation"]; $birthday = $_POST["birthday"]; $db = new mysqli("localhost","root","","12345"); $sql = "update info set name='{$name}',sex={$sex},nation='{$nation}',birthday='{$birthday}' where code='{$code}'"; $r = $db->query($sql); if($r) { header("location:xinxi.php"); } else { echo "修改失敗"; } ?>
顯示結果:
修改成: