php原生的增删该查

来源:互联网 发布:ab post json请求 编辑:程序博客网 时间:2024/05/01 01:51
<?php 
//第一步:链接数据库 
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql链接失败"); 
//第二步: 选择指定的数据库,设置字符集 
@mysql_select_db("php_blog",$conn) or die ("db链接失败".mysql_error()); 
mysql_query('SET NAMES UTF8')or die ("字符集设置错误"); 
?> 

2.增加 add.php 
复制代码代码如下:

<?php 
include("conn.php");//引入链接数据库 
if(!empty($_POST['sub'])){ 
$title=$_POST['title']; 
$con=$_POST['con']; 
echo $sql="insert into news(id,title,dates,contents) value (null,'$title',now(),'$con')" ; 
mysql_query($sql); 
echo"插入成功"; 

?> 
<form action="add.php" method="post"> 
标题: <input type="text" name="title"><br> 
内容: <textarea rows="5" cols="50" name="con"></textarea><br> 
<input type="submit" name="sub" value="发表"> 
</form> 

3.删除del.php 
复制代码代码如下:

<?php 
include("conn.php");//引入链接数据库<pre name="code" class="html"><?php 
include("conn.php");//引入链接数据库 
if(!empty ($_GET['id'])){ 
$sql="select * from news where id='".$_GET['id']."'"; 
$query=mysql_query($sql); 
$rs=mysql_fetch_array($query); 

if(!empty($_POST['sub'])){ 
$title=$_POST['title']; 
$con=$_POST['con']; 
$hid=$_POST['hid']; 
$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 "; 
mysql_query($sql); 
echo "<script> alert('更新成功'); location.href='index.php'</script>"; 
echo"更新成功"; 

?> 
<form action="edit.php" method="post"> 
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/> 
标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br> 
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br> 
<input type="submit" name="sub" value="发表"> 
</form></pre><br> 
if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "删除成功"; ?><p></p> 
<pre></pre> 
<br> 
4,改 edit.php页面 
<p></p> 
<p><br> 
</p> 
<p></p><pre name="code" class="html"><?php 
include("conn.php");//引入链接数据库 
if(!empty ($_GET['id'])){ 
$sql="select * from news where id='".$_GET['id']."'"; 
$query=mysql_query($sql); 
$rs=mysql_fetch_array($query); 

if(!empty($_POST['sub'])){ 
$title=$_POST['title']; 
$con=$_POST['con']; 
$hid=$_POST['hid']; 
$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 "; 
mysql_query($sql); 
echo "<script> alert('更新成功'); location.href='index.php'</script>"; 
echo"更新成功"; 

?> 
<form action="edit.php" method="post"> 
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/> 
标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br> 
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br> 
<input type="submit" name="sub" value="发表"> 
</form></pre><br> 
5.查,列表页面<pre name="code" class="html"><a href="add.php">添加内容</a> 
<hr> 
<hr> 
<form> 
<input type="text" name="keys" /> 
<input type="submit" name="subs" value="搜索"/> 
</form> 
<?php 
include("conn.php");//引入链接数据库 
if(!empty($_GET['keys'])){ 
$w=" title like '%".$_GET['keys']."%'"; 
}else{ 
$w=1; 

$sql="select * from news where $w order by id desc"; 
$query=mysql_query($sql); 
while($rs=mysql_fetch_array($query)){ 
?> 
<h2>标题:<a href="view.php?id=<?php echo $rs['id'] ?>"><?php echo $rs['title'] ?></a> <a href="edit.php?id=<?php echo $rs['id'] ?>">编辑</a>||<a href="del.php?del=<?php echo $rs['id'] ?>">删除</a></h2> 
<li><?php echo $rs['dates'] ?></li> 
<p><?php echo $rs['contents'] ?></p> 
<hr> 
<?php 

?> 
</pre><br> 
<p></p> 
<p><br> 
</p> 
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小学二年级厌学怎么办 做题做的烦躁了怎么办 英语不会做题怎么办 孩子审题不细心怎么办 生孩子有恐惧症怎么办 小孩不喜欢上幼儿园怎么办 小孩不喜欢去幼儿园怎么办 小孩不喜欢学生字怎么办 学生不喜欢补课老师怎么办 小孩不喜欢上学逃课怎么办 一年级小朋友不爱写字怎么办 幼儿园小朋友不爱写字怎么办 孩子懒散不积极怎么办 孩子不肯上幼儿园怎么办 孩子不肯去幼儿园怎么办 小孩记不住字怎么办 小孩不会写字要怎么办 一年级孩子不爱写字怎么办 一年级小孩不爱写字怎么办 孩子不爱写字怎么办呢 幼儿园孩子不爱写字怎么办 孩子上学没学籍怎么办 孩子上学务工证怎么办 孩子上学被欺负怎么办 孩子害怕上幼儿园怎么办 孩子写字肩膀疼怎么办 5岁不会写字怎么办 上中班不爱写字怎么办 孩子性子太慢怎么办 13小孩特别懒怎么办 小孩不肯上幼儿园怎么办 宝宝不肯上幼儿园怎么办 宝宝不肯去幼儿园怎么办 小孩子不肯去幼儿园怎么办 上幼儿园不说话怎么办 小孩写字不认真怎么办 游戏打开是乱码怎么办 小孩不写字该怎么办 小孩不喜欢穿袜子怎么办 宝宝不喜欢穿袜子怎么办 看到婆婆就烦怎么办