这是一些最基础的php增删该查,自我总结,方便以后复习回顾![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)
form.php(表单提交)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title></title> </head> <body> <form method="post" action="sql.php"> <h4>留言板</h4> <table> <tr> <td>请选择昵称:</td> <td><?php include('select.php')?></td> </tr> <tr> <td>留言内容:</td> <td><textarea name="text" rows="5" cols="10"></textarea></td> </tr> <tr> <td colspan=2><input type="submit" value="提交留言"></td> </tr> </table> </form> </body> </html>
sql.php(添加入库)
<?php header("content-type:text/html;charset=utf-8"); //接收表单值 empty($_POST['names'])?$names="":$names=$_POST['names']; empty($_POST['text'])?$text="":$text=$_POST['text']; /* 连接数据库 选择数据库 写sql语句 选择 */ mysql_connect("127.0.0.1","root","1111")or die('连接失败'); mysql_select_db("test")or die('选择失败'); $sql="insert into message(content,nid)values('$text','$names')"; //echo $sql;die; $ra=mysql_query($sql); if($ra){ echo ("<script>alert('添加成功');location.href='list.php'</script>"); } else{ echo ("<script>alert('添加失败');location.href='list.php'</script>"); } ?>
list.php(展示)
<?php header("content-type:text/html;charset=utf-8"); /* 连接数据库 选择数据库 写sql语句 选择 */ mysql_connect("127.0.0.1","root","1111")or die('连接失败'); mysql_select_db("test")or die('选择失败'); $sql="select * from nickname"; //echo $sql;die; $re=mysql_query($sql); //将变量变成二维数组 while($arr=mysql_fetch_assoc($re)){ $new_arr[]=$arr; } //将二维数组循环到下拉列表中 ?> <select name="names"> <?php foreach($new_arr as $k=>$v){ ?> <option value="<?php echo $v['nid']?>"><?php echo $v['name']?></option> <?php } ?> </select>
delete.php(删除)
<?php
header("content-type:text/html;charset=utf-8");
$id=$_GET['id'];
/*
连接数据库
选择数据库
写sql语句
选择
*/
mysql_connect("127.0.0.1","root","1111")or die('连接失败');
mysql_select_db("test")or die('选择失败');
$sql="delete from message where mid='$id'";
//echo $sql;die;
$re=mysql_query($sql);
if($re){
echo ("<script>alert('删除成功');location.href=' list.php'</script>");
}
else{
echo ("<script>alert('删除失败');location.href=' list.php'</script>");
}
?>
update1.php(修改----查询修改内容)
<?php header("content-type:text/html;charset=utf-8"); $cc=$_GET['cc']; /* 连接数据库 选择数据库 写sql语句 选择 */ mysql_connect("127.0.0.1","root","1111")or die('连接失败'); mysql_select_db("test")or die('选择失败'); $sql="select * from message where mid='$cc'"; //echo $sql;die; $re=mysql_query($sql); //将变量变成二维数组 $arr=mysql_fetch_assoc($re); ?> <pre name="code" class="php"><meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <form method="post" action="update.php"> <h4>修改板</h4> <table> <tr> <td>请选择昵称:</td> <td><?php include('select.php')?></td> </tr> <tr> <td>留言内容:</td> <td><textarea name="text" rows="5" cols="10"></textarea></td> </tr> <tr> <td colspan=2><input type="submit" value="确认修改"></td> </tr> </table> <input type="hidden" name="xg" value="<?php echo $arr['mid']?>"> </form>
update.php(修改----修改操作)
<?php header("content-type:text/html;charset=utf-8"); //接收表单值 empty($_POST['names'])?$names="":$names=$_POST['names']; empty($_POST['text'])?$text="":$text=$_POST['text']; empty($_POST['xg'])?$xg="":$xg=$_POST['xg']; /* 连接数据库 选择数据库 写sql语句 选择 */ mysql_connect("127.0.0.1","root","1111")or die('连接失败'); mysql_select_db("test")or die('选择失败'); $sql="update message set name='$names',content='$text' where mid='$xg'"; $re=mysql_query($sql); if($re){ echo ("<script>alert('修改成功');location.href=' list.php'</script>"); } else{ echo ("<script>alert('修改失败');location.href=' list.php'</script>"); } ?>