php增删改查示例代码

增删改查是对数据库最基本的操作,如何连接数据库以及增删改查等等相关知识。

1.链接数据库通用方法:conn.php 
<?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> 
<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 
} 
?>
参考链接:
php数据库操作类 表增删改查、取行数、查询
  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值