当前位置:小鱼儿玄机二站 > 操作系统 > php增加和删除改查示例本身写的demo,MySQL实现简

php增加和删除改查示例本身写的demo,MySQL实现简

文章作者:操作系统 上传时间:2019-10-02

透过php+mysql 实现的简易blog,能够兑现增加和删除改查。效果如下图:

1.链接数据库通用方法:conn.php

图片 1

复制代码 代码如下:

一、数据库及表结构

<?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 ("字符集设置错误");
?>

数据库:test

2.增加 add.php

表:micro_blog(仅独有五个表)字段:id,title,date,content,hits

复制代码 代码如下:

表结构如下:

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

CREATE TABLE `micro_blog` (
  `id` int(20) unsigned NOT NULL AUTO_INCREMENT,
  `title` text NOT NULL ,
  `content` longtext NOT NULL,
  `date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `hits` int(20) DEFAULT 0,
   PRIMARY KEY (`ID`)
)ENGINE=MyISAM DEFAULT CHARSET=utf8;

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>

文件  描述
default.php  暗中同意主页。突显博文与操作连接。
add.php  加多新博文的功用模块。
edit.php  对已经增加过的博文进行改变操作。
delete.php  删除博文模块。
view.php  展现博文的详细音讯(标题|增加日期|浏览次数|内容)。
conn.php  链接数据库操作。在别的文件中被引述。
conn.php

你只怕感兴趣的稿子:

  • PHP+MYSQL完毕客户的增加和删除改查
  • php中PDO方式落成数据库的增加和删除改查
  • php+mysql完毕简单的增加和删除改查功效
  • php中操作memcached缓存实行增加和删除改查数据的兑当代码
  • php数据访问之增加和删除改查操作
  • PHP MySql增加和删除改查的回顾实例
  • php完结数据库的增加和删除改查

<?php
//连接MySql数据库服务
$conn = @mysql_connect("localhost:3306","root","www.361way.com") or die("连接数据库服务器退步!");
//连接ly_php_base数据库
@mysql_select_db("test",$conn) or die("未能连接到数据库!");
mysql_query("SET NAMES 'UTF8'");
?> 
注:后边的set names utf8,假诺不实践,会油然则生插入数据库中的汉字会产生乱码。

default.php

<?php
include("conn.php");
//搜索关键字的管制
if(!empty($_GET['keys'])){
    $keys = "WHERE title like '%".$_GET['keys']."%'";
} else {
    $keys = "";
}
$sql = "SELECT * FROM micro_blog ".$keys." ORDER BY id DESC LIMIT 10";
$query = mysql_query($sql);
$rs = mysql_fetch_array($query);
?>

<html>
<head>
<title>小编的博客园客主页</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
</head>
<body>
<a href="add.php">增加源委</a>
<form action="" method="get">
  <input type="text" name="keys"/>
  <input type="submit" name="submit" value="内容找出"/>
</form>
<hr color="#FF9900" size="3" />
<?php
if(!$rs){
    echo "未有有关内容!";
}
//未有落到实处分页导航作用
while($rs){
?>
<h2>标题:<?php echo $rs['title'];?>|<a href="edit.php?id=<?php echo $rs['id'];?>">编辑</a>|<a href="delete.php?id=<?php echo $rs['id'];?>">删除</a></h2>
<li>日期:<?php echo $rs['date'];?></li>
<p>内容<?php echo iconv_substr($rs['content'],0,50,"UTF-8");?>...... <a href="view.php?id=<?php echo $rs['id'];?>">|查看详细内容|</a></p>
<hr color="#0033FF" size="5" />
<?php
    $rs = mysql_fetch_array($query);
}
?>
</body>
</html>
 
add.php

<?php
//引进连接数据库文件
include("conn.php");
if(!empty($_POST['submit'])){
    $title = $_POST['title'];
    $content = $_POST['content'];
    $sql = "INSERT INTO micro_blog VALUES(NUll,'$title','$content',now(),0)";
    mysql_query($sql);
}
?>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>公布和讯页面</title>
</head>
<body>
<a href="default.php">查看内容</a>
<hr color="#0033CC" size="3px"/>
<form action="add.php" method="post">
  标题:
  <input type="text" name="title"/>
  <br />
  内容:
  <textarea rows="5" cols="50" name="content"></textarea>
  <br />
  <input type="submit" name="submit" value="提交"/>
  <br />
</form>
</body>
</html> 

edit.php

<?php
include("conn.php");
if(!empty($_GET['id'])){
    $id = $_GET['id'];
    $sql = "select * from micro_blog where id = ".$_GET['id'];
    $query = mysql_query($sql);
    $rc = mysql_fetch_array($query);
}
if(!empty($_POST['update'])){
    echo "更新按键提交成功!";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>编辑页面</title>
</head>
<body>
<form action="edit.php?id=<?php echo $id;?>" method="post">
  标题:
  <input type="text" name="title" value="<?php echo $rc['title'];?>"/>
  <br />
  内容:
  <textarea rows="5" cols="50" name="content"><?php echo $rc['content'];?></textarea>
  <br />
  <input type="submit" name="update" value="更新"/>
  <br />
</form>
</body>
</html> 

delete.php

<?php
include("conn.php");
if(!empty($_GET['id'])){
            mysql_query("delete from micro_blog where id =".$_GET['id']);
} else {
        echo "参数引进败北!";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><?php echo $rc['title'];?>|小编的博客园客</title>
</head>
<body>
<a href="default.php">重返主页面</a>
<hr color="#00FFFF" size="5px"/>
<h2><?php echo $rc['title'];?>
  <hr color="#006699" size="3px">
</h2>
<li><?php echo "日期:".$rc['date']."|浏览次数:".$rc['hits'];?></li>
<p><?php echo $rc['content'];?></p>
</body>
</html> 

view.php

<?php
include("conn.php");
if(!empty($_GET['id'])){
    $sql = "SELECT * FROM micro_blog WHERE id = ".$_GET['id'];
    $rc = mysql_fetch_array(mysql_query($sql));
    mysql_query("UPDATE micro_blog SET hits = hits + 1 WHERE id = ".$_GET['id']);
} else {
    echo "参数引进战败!";
}
?>

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title><?php echo $rc['title'];?>|小编的搜狐客</title>
</head>
<body>
<a href="default.php">再次来到主页面</a>
<hr color="#00FFFF" size="5px"/>
<h2><?php echo $rc['title'];?>
  <hr color="#006699" size="3px">
</h2>
<li><?php echo "日期:".$rc['date']."|浏览次数:".$rc['hits'];?></li>
<p><?php echo $rc['content'];?></p>
</body>
</html> 

本文由小鱼儿玄机二站发布于操作系统,转载请注明出处:php增加和删除改查示例本身写的demo,MySQL实现简

关键词: