php增删改查示例自己写的demo
前端之家收集整理的这篇文章主要介绍了
php增删改查示例自己写的demo,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.链接数据库通用方法:conn.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="76810" class="copybut" id="copybut76810" onclick="doCopy('code76810')"> 代码如下:
<div class="codebody" id="code76810">
<?
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 <div class="codetitle">
<a style="CURSOR: pointer" data="68524" class="copybut" id="copybut68524" onclick="doCopy('code68524')"> 代码如下: <div class="codebody" id="code68524">
<?
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">
内容: <textarea rows="5" cols="50" name="con">
<input type="submit" name="sub" value="发表">
3.
删除del.
PHP <div class="codetitle">
<a style="CURSOR: pointer" data="46498" class="copybut" id="copybut46498" onclick="doCopy('code46498')"> 代码如下: <div class="codebody" id="code46498">
<?
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 "";
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']?>">
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?>
<input type="submit" name="sub" value="发表">
if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=MysqL_query($sql); echo "删除成功"; ?>
4,改 edit.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 "";
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']?>">
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?>
<input type="submit" name="sub" value="发表">
5.查,
列表页面<pre name="code" class="html"><a href="add.php">添加内容
<?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)){
?>
标题:<a href="view.php?id=<?php echo $rs['id'] ?>"><?php echo $rs['title'] ?> <a href="edit.php?id=<?php echo $rs['id'] ?>">编辑||<a href="del.php?del=<?php echo $rs['id'] ?>">删除
<?php echo $rs['dates'] ?> <?php echo $rs['contents'] ?>
<?
PHP }
?>