使用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).在 ...
随机推荐
- IIS出现Server Application Unavailable的解决办法
在XP中IIS5.1服务器,运行asp.net出现Server Application Unavailable,经过多次查证,才发现是因为先安装了.net Framework 2.0然后安装IIS,导 ...
- c# 委托 delegate
委托是一种存储函数引用的类型,在事件和事件的处理时有重要的用途 通俗的说,委托是一个可以引用方法的类型,当创建一个委托,也就创建一个引用方法的变量,进而就可以调用那个方法,即委托可以调用它所指的方法. ...
- android MVC理解
算来学习Android开发已有2年的历史了,在这2年的学习当中,基本掌握了Android的基础知识.越到后面的学习越感觉困难,一来是自认为android没啥可学的了(自认为的,其实还有很多知识科学), ...
- ajax.request函数使用详解
Ajax.Request ? Ajax.Request( url, { method:method, parameters:para, postBody:xmlString, asynchrono ...
- CSS 3 属性学习 —— 2. RGBA
RGBA 是 CSS3 中用来控制颜色的单位,分别是 Red Green Blue 三原色和 Alpha 透明度的缩写. 也就是说该属性可以帮助我们在设置颜色的同时,也设置了其透明度. 其实就是 RG ...
- [转]用Gson来解析Json数据
转自太阳尚远的博客:http://blog.yeqianfeng.me/2016/03/02/use_gson_to_parse_json/ 在我们实际开发中像Json和XML这两种格式的数据是最常见 ...
- 超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上
超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上 阅读目录 Deft简介 Deft 核心类介绍 Deft 3分钟即可上手使用 其他可选的配置参数 性能测试 Demo代码下载 回到顶 ...
- python--执行文件的绝对路径
1.__file__属性对应文件名. 2.os.path.realpath(__file__)这样就可以得到文件的绝对路径.
- Example: Develop Web application on Baidu App Engine using CherryPy
In the past few months, I have developed two simple applications on Baidu App Engine. Compared to Go ...
- Delphi 实现无窗口移动(发WM_NCHITTEST消息计算,然后再发WM_SYSCOMMAND消息,带参数SC_DRAGMOVE)
procedure imgListMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer) ...