我做了一个php程序,将表单数据添加到数据库,借用mysql扩展库函数实现对mysql数据库的操作,能够实现添加单词、删除单词、更新和查询单词。运行环境是普通的mysql数据库和php、Apache服务器。这个程序非常简单,属于那种一看就懂的程序,不过还是要提醒一句,像那个数据库和数据表肯定事先要存在!本文用的数据库是test,数据表示test数据库下的表名为danci的数据表,一共有三个属性:id danci dt 分别是int、char、timestamp类型。直接贴代码:

用户看到的界面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>www.codeke.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="keywords" content="php video,javascript" />
<meta http-equiv="description" content="php php php hp php" />
<style type="text/css">
<!--
span { margin:1px 5px;}
-->
</style>
</head>
<form action="pwd.php" method="post">
<table cellpadding="0" cellspacing="0" border="1px" cols="5" width="300">
<tr><td align="center" colspan="2">列表</td><td colspan="2" align="center">名称</td></tr>
<tr><td align="center" valign="bottom" height="20px" rowspan="2" colspan="3">输入单词</td>
<td colspan="2" rowspan="2" valign="middle" height="20px" align="right">
<input size="26" type="text" name="danci" /></td></tr>
<tr></tr>
<tr id="thanks" style="display:none"><td colspan="5"><span>输入旧单词</span><span><input size="26" type="text" name="jiudanci" value="" /></span></td></tr>
<tr></tr>
<tr><td align="center" valign="bottom" height="20px" colspan="3" >选择操作类型</td>
<td valign="middle" height="20px" align="right">
<select name="select" id="opt" onchange="javascript:change();">
<option value="1" selected="selected">添加</option>
<option value="2">删除</option>
<option value="3">更改</option>
<option value="4">查询</option>
</select>
</td>
</tr>
<script type="text/javascript">
window.onload=change;
function change(){
var temp = document.getElementById("opt").value;
if(temp==3){
document.getElementById("thanks").style.display="table-row";
}else{
document.getElementById("thanks").style.display="none";
}
}
</script>
<tr><td colspan="2"><input type="reset" /></td><td align="center" colspan="2"><input type="submit" value="submit" /></td></tr>
</table>
</form>

处理表单数据的php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>www.codeke.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!--meta http-equiv="refresh" content="3;url=access.htm" /-->
</head>
<?php
require_once 'def.class.php'; //这个文件用于数据库操作
$danci=$_POST['danci'];
$jiudanci=$_POST['jiudanci'];
$choice=$_POST['select']; $danci=strtoupper($danci); //将单词统一转化成大写的
$jiudanci=strtoupper($jiudanci);
$sqlQue=new SqlDiy("like","admin");
$sqlQue->setDanci($danci);
$sqlQue->setJiudanci($jiudanci);
switch($choice){
case 1:$sqlQue->setOper("insert");break;
case 2:$sqlQue->setOper("delete");break;
case 3:$sqlQue->setOper("update");break;
case 4:$sqlQue->setOper("select");break;
}
if($danci==$jiudanci&&$choice==3){
echo "新单词和旧单词一样,无需更新"; }
if($danci==""||($jiudanci==""&&$choice==3)){
echo "输入为空";
return ;
} $sqlQue->connSql(); ?>

封装了连接数据库的类php文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>www.codeke.com</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<?php //header("content-type:text/html;charset=utf-8");
class SqlDiy{
private $time;
private $pwd;
private $danci;
private $user;
private $jiudanci;
private $oper;
function __construct($user,$pwd){
$this->user=$user;
$this->pwd=$pwd;
$this->timeWorkErr();
}
function timeWorkErr(){
$this->time=time();
date_default_timezone_set('PRC');
}
function connSql(){ $conn=mysql_connect("localhost",$this->user,$this->pwd);
if(!$conn){
echo mysql_error().error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
}
mysql_select_db("test",$conn) or error_log(mysql_error.date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
mysql_query("set names utf8");
switch($this->oper){
case "select":$query="select * from `danci` where danci='$this->danci' limit=0,1";break; //一般不要用*,需要哪个字段就选择哪个字段
case "delete":$query="delete from `danci` where danci='$this->danci'";break;
case "insert":$query="insert into `danci`(danci)values('$this->danci')";break;
case "update":$query="update `danci` set `danci`='$this->danci' where `danci`='$this->jiudanci'";break;
}
//$query="update user set name='lily' where name='lily'";
mysql_query("select * from `danci` where danci='$this->danci'",$conn);
if(mysql_affected_rows($conn)>0&&$this->oper=="insert"){
echo "<br />单词库中已经有该单词,无需添加";
return ;
}
if(mysql_affected_rows($conn)<1&&$this->oper=="select"){
echo "<br />单词库里没有该单词"; //针对查询操作
return ;
}
if(mysql_affected_rows($conn)<1&&$this->oper=="delete"){
echo "<br />无法删除一个没有的单词"; //针对删除操作
return ;
}
$res=mysql_query($query,$conn);
if(mysql_affected_rows($conn)<1){
echo "<br />连接数据库有误$this->jiudanci--$this->danci";
error_log(mysql_error().date("m月d日 H:i:s",$this->time)."\r\n",3,"c:/error.txt");
}
else {
if($this->oper=="select"){
while($rows=mysql_fetch_object($res)){
echo "<br />".$rows->id."--".$rows->danci."--".$rows->dt;
}
mysql_free_result($res);
}
if($this->oper=="insert"){
echo "<br />插入单词成功";
}
if($this->oper=="delete"){
echo "<br />删除单词成功";
}
if($this->oper=="update"){
echo "<br />更新单词成功";
}
}
mysql_close($conn);
}
function setJiudanci($i){
$this->jiudanci=$i;
}
function setOper($i){
$this->oper=$i;
}
function setDanci($i){
$this->danci=$i;
} } ?>

建立数据库的函数:

function createDataBase(){ //建立一个数据表
$link=mysql_connect('localhost',"root",$password);//必须用root账号和密码登录
if(!$link){
die('Could not connect: ' . mysql_error());
}
$sql = 'CREATE DATABASE my_db';
if(mysql_query($sql, $link)) {
echo "Database my_db created successfully\n";
} else {
echo 'Error creating database: '.mysql_error()."\n";
} mysql_close($link);
}

删除数据库的函数:

function deleteDataBase(){ //删除一个数据表
$link=mysql_connect('localhost',"root",$password);//必须用root账号和密码登录
if(!$link){
die('Could not connect:'.mysql_error());
}
$sql = 'DROP DATABASE discuz';
if(mysql_query($sql, $link)) {
echo "Database discuz droped successfully\n";
} else {
echo 'Error droping database: '.mysql_error() . "\n";
} mysql_close($link);
}

由mysql数据库基础上的php程序实现单词的查询、删除、更改和查询的更多相关文章

  1. MySQL数据库基础

    MySQL数据库基础 本文的所有操作是基于CMD环境,MySQL通过在命令行中输入SQL语句对数据库进行操作.配置问题可参考<打通MySQL的操作权限>中的内容,该文算是针对前期的环境配置 ...

  2. php面试专题---15、MySQL数据库基础考察点

    php面试专题---15.MySQL数据库基础考察点 一.总结 一句话总结: 注意:只写精品 1.mysql定义int(3),那么我存1234就错了么? 不是:无影响:只会影响显示字符的个数:可以为整 ...

  3. Linux下自动备份MySQL数据库并上传到远程FTP服务器

    Linux下自动备份MySQL数据库并上传到远程FTP服务器且删除指定日期前的备份Shell脚本 说明:  1.备份MySQL数据库存放目录/var/lib/mysql下面的xshelldata数据库 ...

  4. Mysql数据库基础学习笔记

    Mysql数据库基础学习笔记 1.mysql查看当前登录的账户名以及数据库 一.单表查询 1.创建数据库yuzly,创建表fruits 创建表 ) ) ,) NOT NULL,PRIMARY KEY( ...

  5. PHP面试 MySQL数据库基础

    MySQL数据库基础 MySQL数据类型 整数类型:TINYINT   SMALLINT   MEDIUMINT   INT   BIGINT 属性:UNSIGNED 长度:可以为整数类型指定宽度,列 ...

  6. MySQL数据库--基础简述

    MySQL数据库--基础简述 1.15.1 MySQL简介 Mysql是最流行的RDBMS(Relational Database Management System:关系数据库管理系统),特别是在W ...

  7. MySQL数据库基础知识及优化

    MySQL数据库基础知识及优化必会的知识点,你掌握了多少? 推荐阅读: 这些必会的计算机网络知识点你都掌握了吗 关于数据库事务和锁的必会知识点,你掌握了多少? 关于数据库索引,必须掌握的知识点 目录 ...

  8. 26.MySQL数据库基础

    MySQL数据库基础 目录 MySQL数据库基础 数据库的概念 数据 表 数据库 数据库的管理系(DBMS) 数据库系统 访问数据库的流程 数据库系统发展史 当今主流数据库介绍 关系数据库 关系数据库 ...

  9. mysql数据库基础的简单操作指南

    最近在学习mysql,本文是做的关于mysql学习的笔记,跟大家分享一下,希望对大家学习mysql知识有所助益.mysql现在几乎已经成了网站建设的主流数据库,很多php网站系统都采用了mysql数据 ...

随机推荐

  1. hdu4893Wow! Such Sequence! (线段树)

    Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...

  2. passwd-shadow文件

    [root@rusky /]# vi /etc/passwd root:x:::Redhat5:/root:/bin/bash rusky:x::::/home/rusky:/bin/bash 1.r ...

  3. 移动端(IOS)iframe监听不到 onscroll 事件

    问题描述: 我在一个页面A中有瀑布流,点击瀑布流中的图片需要进入到另外一个页面B,点击返回需要回到页面A中点击的位置,为了实现该效果所以在页面A中嵌入iframe,iframe指向页面B,页面B中同样 ...

  4. 基于Spring MVC的简单HelloWorld实例

    1.导包 2.web.xml文件配置 3.包结构定义以及控制器的编写 4.xxxx-servlet文件配置 5.返回的视图(jsp)编写   6.源码 下载:http://download.csdn. ...

  5. (转)sp_executesql介绍和使用

    execute相信大家都用的用熟了,简写为exec,除了用来执行存储过程,一般都用来执行动态Sql sp_executesql,sql2005中引入的新的系统存储过程,也是用来处理动态sql的, 如: ...

  6. 关于textbox.attributes["value"]的问题

    在“修改”时,出现这个问题,后台点击修改时,应该是文本框出现一些初始值 BLL.manager bll = new BLL.manager(); Model.manager model = bll.G ...

  7. 【转】使用 Eclipse 调试 Java 程序的 10 个技巧

    你应该看过一些如<关于调试的N件事>这类很流行的帖子 .假设我每天花费1小时在调试我的应用程序上的话,那累积起来的话也是很大量的时间.由于这个原因,用这些时间来重视并了解所有使我们调试更方 ...

  8. Fragment实现底部Tab,切换可保存状态

    activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

  9. SQL GROUP BY 语句

    合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句. GROUP BY 语句 GROUP BY 语句用于结合合计函数,根据一个或多个列对结果集进行分组. SQL GROUP BY 语法 ...

  10. C++ Primer 5th 第6章 函数

    正如第一章所说:C++的函数是一个能够完成一个功能的模块或者说是一段命名了的代码块. 如下图所示,函数可以重载,是一段实现某些功能命名了的代码. 一个完整的函数的构成有四部分: 1.返回类型 2.函数 ...