使用mysql_query()方法操纵数据库以及综合实例
1.利用insert 语句添加记录
<? require('conn.php');
mysql_query( "insert into lyb ( title, content, author, email,`date`) values ('大家好', 'PHP学习园地', '小浣熊', 'sdf@sd.com','2012-3-3')") or die('执行失败');
echo '新增记录的id是'.mysql_insert_id(); //可选,输出新记录的id
?>
2.利用delete语句删除数据
<? require('conn.php');
mysql_query( " Delete from lyb where ID in(158,162,163,169)") or die('执行失败');
?>
本次操作共有<?= mysql_affected_rows() ?>条记录被删除!
3.利用updata语句更新数据
<? require('conn.php');
mysql_query("Update lyb set email='rong@163.com', author='蓉蓉' where ID>133 and ID<143") or die('执行失败');
?>
接下来演示一个完整的例子
5.1.php为插入页面
<?php
/*
*
* @Authors peng--jun
* @Email 1098325951@qq.com
* @Date 2015-11-07 13:50:48
* @Link http://www.cnblogs.com/xs-yqz/
* @version $Id$
==========================================
*/
header("Content-type: text/html; charset=UTF-8"); if (isset($_POST['submit'])) {
require("include/conn.php");
mysql_select_db("lyb",$conn);//选择数据库 $title = $_POST['title'];
$author = $_POST['author'];
$content = $_POST['content'];
$email = $_POST['email'];
$result = mysql_query("insert into `lyb1`(`id`,`title`,`content`,`author`,`email`) values(null,'$title','$content','$author','$email')")or die('执行失败');;
var_dump($result);
echo "新增的记录的id为".mysql_insert_id();
mysql_free_result($result);
mysql_close($result);
echo "<script>alert('插入完成');</script>";
header("Location:5.6.php");
/* header("refresh:3;url=5.6.php");*/ //3秒后 页面跳转
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加数据页面</title>
</head>
<body>
<form action="5.1.php" method="POST">
<p>添加新闻页面</p>
<div>标题:<input type="text" name="title" id=""></div>
<div>内容: <textarea name="content" id="" cols="" rows=""></textarea></div>
<div>作者:<input type="text" name="author" id=""></div>
<div>邮箱:<input type="text" name="email" id=""></div>
<div><input type="reset" value="重置"><input type="submit" name="submit" value="提交"></div>
</form>
</body>
</html>
5.6.php为从数据库后台提取数据 显示在前台的页面
<?php
/*
*
* @Authors peng--jun
* @Email 1098325951@qq.com
* @Date 2015-11-07 15:10:04
* @Link http://www.cnblogs.com/xs-yqz/
* @version $Id$
==========================================
*/
header("Content-type: text/html; charset=UTF-8");
require("include/conn.php");
mysql_select_db("lyb",$conn);//选择数据库
$result = mysql_query("select * from `lyb1`",$conn);//选择数据库表
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<a href="5.1.php">添加记录</a>
<table border="">
<tr bgcolor="#ccc">
<th>序号</th>
<th>标题</th>
<th>内容</th>
<th>作者</th>
<th>邮箱</th>
<th>删除</th>
<th>更新</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?= $row['id']?></td>
<td><?= $row['title']?></td>
<td><?= $row['content']?></td>
<td><?= $row['author']?></td>
<td><?= $row['email']?></td>
<td><a href="delete.php?id=<?= $row['id']?>">删除</a></td>
<td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
</tr>
<?php
}
?>
</table>
<p>共有<?= mysql_num_rows($result) ?>条记录 </p>
<!-- mysql_num_rows()函数返回的是结果集的总数 -->
<?php
//释放资源,关闭结果集
mysql_free_result($result);
mysql_close($result);
?> </body>
</html>
delete.php删除程序
<?php
/*
*
* @Authors peng--jun
* @Email 1098325951@qq.com
* @Date 2015-11-07 15:26:27
* @Link http://www.cnblogs.com/xs-yqz/
* @version $Id$
==========================================
*/
header("Content-type: text/html; charset=UTF-8");
require("include/conn.php");
mysql_select_db("lyb",$conn);//选择数据库
$id = intval($_GET['id']);//获取5-6.php传来的id参数并转换为整型
$sql = "delete from `lyb1` where `id` = $id ";//删除指定的内容
if (mysql_query($sql) && mysql_affected_rows()==) {//执行sql语句并判断执行是否成功
echo "<script>alert('删除成功!');location.href='5.6.php'</script>";
}else{
echo "<script>alert('删除失败!');location.href='5.6.php'</script>";
}
?>

多选删除页面deleteall.php
<?php
/*
*
* @Authors peng--jun
* @Email 1098325951@qq.com
* @Date 2015-11-07 15:10:04
* @Link http://www.cnblogs.com/xs-yqz/
* @version $Id$
==========================================
*/
header("Content-type: text/html; charset=UTF-8");
require("include/conn.php");
mysql_select_db("lyb",$conn);//选择数据库 if ($_GET["del"]==) {//如果用户按了“删除”按钮
$selectid=$_POST["selected"];//获取所有选中多选框的值,保存到数组中
if( count($selectid)>){//防止selectid值为空时执行SQL语句出错
$sel = implode(",", $selectid);//将各个数组元素用“,”号连接起来
mysql_query("delete from `lyb1` where `id` in($sel)")or die("执行失败".mysql_error());
header("Location:deleteall.php");//删除完毕,刷新页面
}else{
echo "没有被选中的数据";
}
} $result = mysql_query("select * from `lyb1`",$conn);//选择数据库表
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form method="post" action="?del=1"> <!--表单提交给自身-->
<a href="5.1.php">添加记录</a>
<table border="">
<tr bgcolor="#ccc">
<th>序号</th>
<th>标题</th>
<th>内容</th>
<th>作者</th>
<th>邮箱</th>
<th>删除</th>
<th>更新</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?= $row['id']?></td>
<td><?= $row['title']?></td>
<td><?= $row['content']?></td>
<td><?= $row['author']?></td>
<td><?= $row['email']?></td>
<td><input type="checkbox" name="selected[]" id="" value="<?= $row['id']?>"></td><!-- 删除的复选框 -->
<td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
</tr>
<?php
}
?>
<tr bgcolor="#ddd">
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td><input type="submit" value="删 除"></td><!-- 删除按钮 -->
<td></td>
</tr>
</table>
<p>共有<?= mysql_num_rows($result) ?>条记录 </p>
</form>
<!-- mysql_num_rows()函数返回的是结果集的总数 -->
<?php
//释放资源,关闭结果集
mysql_free_result($result);
mysql_close($result);
?> </body>
</html>

更新页面的代码 editform.php
<?php
/*
*
* @Authors peng--jun
* @Email 1098325951@qq.com
* @Date 2015-11-07 16:39:42
* @Link http://www.cnblogs.com/xs-yqz/
* @version $Id$
==========================================
*/
header("Content-type: text/html; charset=UTF-8");
require("include/conn.php");
mysql_select_db("lyb",$conn);//选择数据库 $id = intval($_GET['id']);//将获取的id强制转换为整型
$sql = "select * from `lyb1` where id = $id";
echo $sql;
$result = mysql_query($sql,$conn);//选择数据库表
$row = mysql_fetch_assoc($result);//将待更新记录各字段的值存入数组中 if ($_POST["submit"]) {//当单击确认按钮后执行更新语句
$title = $_POST['title'];
$author = $_POST['author'];
$content = $_POST['content'];
$email = $_POST['email'];
$sql_del = "Update `lyb1` Set title='$title',author='$author',email='$email',content='$content' Where id='$id'";
echo $sql_del;
mysql_query($sql_del)or die("执行失败".mysql_error());
echo "<script>alert('留言修改成功');location.href='5.6.php'</script>";
} ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加数据页面</title>
</head>
<body>
<form action="?id=<?= $row['id']?>" method="POST">
<p>添加新闻页面</p>
<div>标题:<input type="text" name="title" id="" value="<?= $row['title']?>"></div>
<div>内容: <textarea name="content" id="" cols="" rows=""><?= $row['content']?></textarea></div>
<div>作者:<input type="text" name="author" id="" value="<?= $row['author']?>"></div>
<div>邮箱:<input type="text" name="email" id="" value="<?= $row['email']?>"></div>
<div><input type="reset" value="重置"><input type="submit" name="submit" value="确定"></div>
</form>
</body>
</html>
查询记录的实现search.php
<?php
/*
*
* @Authors peng--jun
* @Email 1098325951@qq.com
* @Date 2015-11-07 17:31:16
* @Link http://www.cnblogs.com/xs-yqz/
* @version $Id$
==========================================
*/
header("Content-type: text/html; charset=UTF-8");
require("include/conn.php");
mysql_select_db("lyb",$conn);//选择数据库
$result = mysql_query("select * from `lyb1`",$conn);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询页面</title>
</head>
<body>
<form action="search_result.php" method="get">
<div style="border:1px solid gray; background:#eee;padding:4px;">
查找留言:请输入关键字 <input name="keyword" type="text">
<select name="sel">
<option value="title">文章标题</option>
<option value="content">文章内容</option>
</select>
<input type="submit" name="submit" value="查询">
</div>
</form> <table border="">
<tr bgcolor="#ccc">
<th>序号</th>
<th>标题</th>
<th>内容</th>
<th>作者</th>
<th>邮箱</th>
<th>删除</th>
<th>更新</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?= $row['id']?></td>
<td><?= $row['title']?></td>
<td><?= $row['content']?></td>
<td><?= $row['author']?></td>
<td><?= $row['email']?></td>
<td><a href="delete.php?id=<?= $row['id']?>">删除</a></td>
<td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
</tr>
<?php
}
?>
</table>
<p>共有<?= mysql_num_rows($result) ?>条记录</p>
</body>
</html>

查询结果的显示页面search_result.php
<?php
/*
*
* @Authors peng--jun
* @Email 1098325951@qq.com
* @Date 2015-11-07 17:40:21
* @Link http://www.cnblogs.com/xs-yqz/
* @version $Id$
==========================================
*/
header("Content-type: text/html; charset=UTF-8");
require("include/conn.php");
mysql_select_db("lyb",$conn);//选择数据库
$keyword=trim($_GET['keyword']);//获取输入的关键字
$sel=$_GET['sel'];//获取选择的查询类型
$sql="select * from `lyb1`";
if ($keyword<>"") {
$sql=$sql ." where $sel like '%$keyword%'"; //构造查询语句
}
$result=mysql_query($sql) or die('执行失败');
if (mysql_num_rows($result)>) {
echo "<p>关键字为“ $keyword ”,共找到".mysql_num_rows($result)." 条留言</p>"; ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询结果</title>
</head>
<body> <table border="">
<tr bgcolor="#ccc">
<th>序号</th>
<th>标题</th>
<th>内容</th>
<th>作者</th>
<th>邮箱</th>
<th>删除</th>
<th>更新</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
?>
<tr>
<td><?= $row['id']?></td>
<td><?= $row['title']?></td>
<td><?= $row['content']?></td>
<td><?= $row['author']?></td>
<td><?= $row['email']?></td>
<td><a href="delete.php?id=<?= $row['id']?>">删除</a></td>
<td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
</tr>
<?php
}
}else echo "没有搜索到任何留言";
?>
</table>
</body>
</html>

至此,整个所有的操作都完成了。over!!!
使用mysql_query()方法操纵数据库以及综合实例的更多相关文章
- 主窗体里面打开子窗体&&打印饼图《Delphi 6数据库开发典型实例》--图表的绘制
\Delphi 6数据库开发典型实例\图表的绘制 1.在主窗体里面打开子窗体:ShowForm(Tfrm_Print); procedure Tfrm_Main.ShowForm(AFormClass ...
- [原创]MongoDB综合实例一
CentOS-6.5单机实现mongoDB分片 环境:1)CentOS 6.5系统 2)IP:本机3)MongoDB:MongoDB-linux-x86_64-2.6.1 实现:两个副本集s ...
- 基于Nodejs的sequelize操纵数据库
## 使用基于ORM架构的sequelize操纵数据库 ### 1.技术背景 ```Sequelize是一个基于promise的关系型数据库ORM框架,*********************技术文 ...
- 详解:数据库名、实例名、ORACLE_SID、数据库域名、全局数据库名、服务名及手工脚本创建oracle数据库
数据库名.实例名.数据库域名.全局数据库名.服务名 , 这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水.我们现在就来把它们弄个明白. 一.数据库名 什么是数 ...
- ReportingServies——SQLServer报表开发综合实例
如果我们安装了sqlserver2008 R2,将会自动安装一个报表开发工具 不要以为此报表开发工具只适合于sqlserver2008,其实在sqlserver2012中也是支持的,事实上我现在项目中 ...
- Oracle数据库名、实例名、数据库域名、全局数据库名、服务名之间的区别
数据库名.实例名.数据库域名.全局数据库名.服务名 这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水.我们现在就来把它们弄个明白. 一.数据库名 什么是数据库 ...
- 查询oracle数据库的数据库名、实例名、ORACLE_SID
数据库名.实例名.数据库域名.全局数据库名.服务名 , 这是几个令很多初学者容易混淆的概念.相信很多初学者都与我一样被标题上这些个概念搞得一头雾水.我们现在就来把它们弄个明白. 一.数据库名 什么是数 ...
- 面向对象:静态属性,静态方法,组合,继承,衍生,继承之mro线性顺序列表,面向对象综合实例
1.静态属性(附有装饰器) class Room: def __init__(self,name,owner,width,length,height): self.name=name self.own ...
- Oracle 数据库名、实例名、Oracle_SID
本文参考自ORACLE 数据库名.实例名.ORACLE_SID的区别,纯属读书笔记,加深记忆 在ORACLE7.8数据库中只有数据库名(db_name)和数据库实例名(instance_name).在 ...
随机推荐
- node.js querystring处理参数
C:\Documents and Settings\Administrator\WebstormProjects\untitled6>node> url{ parse: [Function ...
- sqlserver数据库三范式的理解
从来都是听过概念,过一段时间就忘记了,根本就没有深入的理解.这次梳理一遍,用自己的方式记录一下. 1nf 原子性,不可拆分性 例如一张表里包含一个class属性(软件系,外语系,经贸系...)字段,这 ...
- Linux下安装McAfee防病毒软件(企业版本)
最近公司接一个项目虚拟化解决方案,不过所有硬件设备不是我们采购的,我们只是负责软体安装.我看了一下那个硬件设备那叫高,不过目前还到那边去安装,那边硬件还没安装完成,然后Boss给我拿来两台新服务器,让 ...
- sql 添加字段备注和查看已添加表的备注
虽然avl树和红黑树在数据搜索和排序方面都是有效的数据结构,但是都显得特别麻烦,跳跃表就显得特别简单,虽然简单 不影响他性能,在平均情况下,其插入.删除.查找数据时间复杂度都是O(log(N)),其最 ...
- 井字棋(Tic-Tac-Toe)
井字棋介绍:https://en.wikipedia.org/wiki/Tic-tac-toe 井字棋简单,但是获胜策略却和直觉不同,四角比中间重要性要高,而且先手有很大的获胜概率获胜(先手胜:91, ...
- 手机访问电脑wampServer本地环境页面
1. 电脑需要安装好wamp,我这里用的2.0版本,下载地址 http://pan.baidu.com/s/1jG31hbS 2. 电脑需要有个wifi,我用的360wifi 3. 启动 ...
- Swift笔记3
赋值运算符" = " let (x,y) =(10,45) var str = "luo" + "shaui" //会得到luoshu ...
- SQL Server 数据文件的页面分部情况
---------------------------------------------------------------------------------------------------- ...
- Delphi下获取IE的UserAgent的方法
方法一:使用SHDocVw, MSHtml单元提供的一些方法利用浏览器的特性来获取. uses SHDocVw, MSHtml; function GetUserAgent: string;var ...
- de4dot命令 v2.0.3.3405
de4dot v2.0.3.3405 Copyright (C) 2011-2013 [email]de4dot@gmail.com[/email] Latest version and source ...