日期:2014-05-16 浏览次数:20599 次
我做了一个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['sele