使用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).在 ...
随机推荐
- Hadoop 安装 (4) SSH无密码验证配置
验证SSH 和 RSYNC 已经安装好了 Master 生成密码对以及对于 Slave 的无密码登录. 见:http://www.cnblogs.com/xia520pi/archive/2012/0 ...
- Python的基础--对象
对象(Objects)是python中数据的抽象,python中所有的数据均可以用对象或者是对象之间的关系来表示.每个对象均有标识符(identity).类型(type).值(value). 标识符. ...
- 设计模式之UML类图
在学设计模式的过程中经常碰到各式各样的UML类图.那些眼花缭乱的符号有什么含义呢? 类图含义 类图中的关系 从网上找来一张图作为实例 依赖关系:比如动物依赖氧气和水,这里如学生要依赖自行车.用虚线箭头 ...
- 把WCF服务部署服务器IIS异常(详细:处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler”)
详细:处理程序“svc-Integrated”在其模块列表中有一个错误模块“ManagedPipelineHandler” 原因: vs2010默认的是4.0框架,4.0的框架是独立的CLR,和2.0 ...
- mysqli扩展库的 预处理技术 mysqli stmt
问题的提出? 现在需要向mysql数据库添加100个用户,请问如何实现? 思路: 使用for循环100次,向数据库中添加100个用户. 使用批量添加 $sql1=”insert xxx”; $ssql ...
- python 网络编程第一版
--version 1.0 只完成server/client 之间的通信. 1.server端的代码: #!/usr/bin/python #!coding:utf-8 from socket imp ...
- Delphi下获取IE的UserAgent的方法
方法一:使用SHDocVw, MSHtml单元提供的一些方法利用浏览器的特性来获取. uses SHDocVw, MSHtml; function GetUserAgent: string;var ...
- 没有产品,没有用户的,绝对不要浪费时间去联系风投——没有过home run的创业人,想办法先做出产品,找到少量用户,没有任何销售成本
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:Kuan Huang链接:http://www.zhihu.com/question/19641135/answer/1353 ...
- openstack中文文档
http://www.openstack.cn/p392.html openStack Hacker中文文档 http://docs.mirantis.com/fuel-dev/develop/a ...
- WMware VMX格式转换成OVF
1.关于VMX格式 vmx文件是vmware虚拟机系统的配置文件,注意:刚刚安装好VMware Workstation以后是找不到这个文件的,当你在VMware Workstation中建立了一个虚拟 ...