日期:2014-05-16  浏览次数:20453 次

PHP增删改查的简单实现

come from http://hi.baidu.com/javashmily/item/69d117fc3ebbdd723d198b9e

<?php
//获得连接
$db = mysql_connect("localhost", "root", "root") or die(mysql_error());
echo "Connected to MySQL<br/>";
//连接数据库
mysql_select_db("php") or die(mysql_error());
echo "Connected to Database";
//增加操作
if ($submit){????????????????????????????????????????????????????????????????
??? $sql = "INSERT INTO student (id,name) VALUES ('$id','$name')";??????????????????
??? $result = MySQL_query($sql);????????????????????????????????????
??? echo "记录添加成功!<p>";?????????????????????????????????
??? }
??? //删除操作
??? if($delete){
??? $sql ="delete from student where id = $id";
??? $result = mysql_query($sql);
??? echo "删除了id为 $id 的记录 ";
??? }
??? //修改操作
??? if($update){
??? $sql = "update student set name='$name' where id = $id";
??? $result = mysql_query($sql);
??? echo "修改成功!!";
??? }
??? //查询数据,并用表格显示出来
??? $result = mysql_query("select * from student",$db);

echo "<table border=1>\n";??????????????????????????????????????
??? echo "<tr><td>id</td><td>姓名</td>";?????????????????????????????????????????????????????
??? echo "</tr>\n";?????????????????????????????????????????????????
??? //循环遍历??????????????????????????????????????????????????????????
while ($myrow = mysql_fetch_row($result)){???????????????????????????????????????????????????????????????
??? printf("<tr><td>%s</td><td>%s</td>", $myrow[0], $myrow[1]);
??? }???????????????????????????????????????????????????????????????
echo "</table>\n";
?>
<html>
<form action="" name="">
?? <input type="text" name="id"/> <br/>
?? <input type="text" name="name"/><br/>
?? <input type="submit" value="提交" name="submit"/>
?? <input type="submit" value="删除" name="delete"/>
?? <input type="submit" value="修改" name="update"/>
</form>
</html>