首页 >后端开发 >php教程 >php增删改查示例自己写的demo_PHP

php增删改查示例自己写的demo_PHP

WBOY
WBOY原创
2016-06-01 12:03:47821浏览

1.链接数据库通用方法:conn.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
复制代码 代码如下:
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"插入成功";
}
?>


标题:

内容:




3.删除del.php
复制代码 代码如下:
include("conn.php");//引入链接数据库
<?php <BR>include("conn.php");//引入链接数据库 <br>if(!empty ($_GET['id'])){ <br>$sql="select * from news where id='".$_GET['id']."'"; <br>$query=mysql_query($sql); <br>$rs=mysql_fetch_array($query); <br>} <br>if(!empty($_POST['sub'])){ <br>$title=$_POST['title']; <br>$con=$_POST['con']; <br>$hid=$_POST['hid']; <br>$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 "; <br>mysql_query($sql); <br>echo "<script> alert('更新成功'); location.href='index.php'</script>"; <br>echo"更新成功"; <br>} <br>?> <br>


标题:

内容:




if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "删除成功"; ?>


 


4,改 edit.php页面





<?php <BR>include("conn.php");//引入链接数据库 <br>if(!empty ($_GET['id'])){ <br>$sql="select * from news where id='".$_GET['id']."'"; <br>$query=mysql_query($sql); <br>$rs=mysql_fetch_array($query); <br>} <br>if(!empty($_POST['sub'])){ <br>$title=$_POST['title']; <br>$con=$_POST['con']; <br>$hid=$_POST['hid']; <br>$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 "; <br>mysql_query($sql); <br>echo "<script> alert('更新成功'); location.href='index.php'</script>"; <br>echo"更新成功"; <br>} <br>?> <br>


标题:

内容:




5.查,列表页面
<a href="add.php">添加内容</a> <br><hr> <br><hr> <br>




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)){
?>

标题: 编辑||删除






  • }
    ?>






    声明:
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn