1、通过php向数据库添加记录
当前页面:
<form action="chuli.php" method="post"> <div>代号:<input type="text" name="code"/></div> <div>姓名:<input type="text" name="name"/></div> <div>民族:<input type="text" name="nation"/></div> <input type="submit" value="提交"/> </form>
php处理页面:
<?php $a=$_POST["code"]; $b=$_POST["name"]; $c=$_POST["nation"]; @$connect=mysql_connect("localhost","root","123"); mysql_select_db("nation",$connect); $sql="insert into nation values('{$a}','{$b}','{$c}')"; $result=mysql_query($sql);//执行sql语句返回结果集,如果是增删改返回bool型 if($result) { header("location:14.php");//返回初始页面 } else { echo "添加失败"; } ?>
2、下拉列表
<select> <?php $connect=mysql_connect("localhost","root","123"); mysql_select_db("nation",$connect); $sql="select nation from nation"; $result=mysql_query($sql); while($attr=mysql_fetch_row($result)) { echo "<option>$attr[0]</option>"; } ?> </select>
3、删除
实现如下效果,点击删除消失当前记录。
代码如下:
当前页面
<h1 align="center">民族表</h1> <table border="1" cellpadding="0" cellspacing="0" width="100%"> <tr> <th>代号</th> <th>姓名</th> <th>民族</th> <th>操作</th> </tr> <?php $connect=@mysql_connect("localhost","root","123"); mysql_select_db("nation",$connect); $sql="select * from nation"; $result=mysql_query($sql); while($attr=mysql_fetch_row($result)) { echo "<tr> <td align='center'>$attr[0]</td> <td align='center'>$attr[1]</td> <td align='center'>$attr[2]</td> <td align='center'><a href='delete.php?code={$attr[0]}'>删除</a></td> </tr>"; }//重点是<a href='delete.php?code={$attr[0]}'>删除</a> ?> </table>
删除页面:
<?php $code=$_GET["code"];//重点 $connect=@mysql_connect("localhost","root","123"); mysql_select_db("nation",$connect); $sql="delete from nation where code='{$code}'"; $result=mysql_query($sql); if($result) { header("location:15.php"); } else{ echo "删除失败"; } ?>
备注:<a href='zhuye.php' onclick="return confirm('确定吗')">{$attr[4]}</a>//返回true或false给<a>标签,不加return是不可以的。