0
php+mysql数据库简单操作
SET NAMES gbk
SET NAMES utf8
- db_config.php
- <?php
- //数据库连接信息
- $db_host="localhost";
- $db_user="root";
- $db_pass="vertrigo";
- $db_name="test";//数据库名字
- $db_table="pay_list";//数据表名字
- $db_charset="gbk";或utf8,没有-
- //登录用户名和密码
- $username="admin";
- $password="520";
- ?>
- connect.php
- <?php
- require_once('db_config.php');
- //连接Mysql失败
- $connect=mysql_connect($db_host,$db_user,$db_pass) or die("连接Mysql失败!n".mysql_error());
- //创建数据库
- //mysql_query("create database $db_name") or die("创建数据库失败!n".mysql_error());;
- //选择数据库
- mysql_select_db($db_name,$connect) or die("选择数据库失败!n".mysql_error());
- //设定字符集
- mysql_query("SET NAMES $db_charset");//设定字符集
- ?>
- 获取数据
- <?php
- $sql="select * from $db_table where id='$id'";
- $query=mysql_query($sql) or die(mysql_error());
- while($rs=mysql_fetch_array($query)){
- //***********
- // 商品基本信息
- /* 商品名称 */
- $subject= $rs["subject"];
- /* 商品描述 */
- $body = $rs["body"];
- /* 商品价格(包含运费),以元¥为单位 */
- $price = $rs["price"];
- }
- ?>
增加记录
- <?php
- include(dirname(__FILE__)."/../config/connect.php");
- //数据库连接
- if (!get_magic_quotes_gpc()) {
- $subject = addslashes($_POST['subject']);
- $price = addslashes($_POST['price']);
- $body = addslashes($_POST['body']);
- $show_url= addslashes($_POST['show_url']);
- $message = addslashes($_POST['message']);
- }
- if(emptyempty($_POST["subject"])||emptyempty($_POST["price"])||emptyempty($_POST["body"])||emptyempty($_POST["show_url"])||emptyempty($_POST["message"])){
- echo ("<script type='text/javascript'> alert('不能有空值,请检查');history.go(-1);</script>");
- exit;
- }
- //数据库插入记录
- $sql="insert into $db_table (subject,price,body,show_url,time,message) values ('$subject','$price','$body','$show_url',now(),'$message')";
- $result=mysql_query($sql) or die(mysql_error());
- if($result)
- {echo ("<script type='text/javascript'> alert('添加成功!即将返回商品列表!');location.href='list.php';</script>");}
删除记录
- <?php
- include(dirname(__FILE__)."/../config/islogin.php");
- //这里是SESSION来验证用户的合法性
- include(dirname(__FILE__)."/../config/connect.php");
- //数据库连接
- if($id=$_GET['id'])//获取参数
- {
- $sql="delete from $db_table where id=$id";
- $delete_result=mysql_query($sql) or die(mysql_error());
- echo ("<script type='text/javascript'> alert('删除成功!即将返回商品列表!');location.href='list.php';</script>");
- }
- else echo ("<script type='text/javascript'> alert('参数错误');history.go(-1);</script>");
- ?>
修改数据
- <?php
- include(dirname(__FILE__)."/../config/islogin.php");
- //这里是SESSION来验证用户的合法性
- include(dirname(__FILE__)."/../config/connect.php");
- //数据库连接
- $id=$_GET["id"];
- //获取要修改的ID
- //提交修改
- if(isset($_POST['Submit'])){
- /*处理特殊字符*/
- if (!get_magic_quotes_gpc()) {
- $subject = addslashes($_POST['subject']);
- $price = addslashes($_POST['price']);
- $body = addslashes($_POST['body']);
- $show_url= addslashes($_POST['show_url']);
- $message = addslashes($_POST['message']);
- }
- /**/
- /*
- $subject = $_POST['subject'];
- $price = $_POST['price'];
- $body = $_POST['body'];
- $show_url= $_POST['show_url'];
- $message = $_POST['message'];
- */
- $sql_edit="update $db_table set subject='$subject',price='$price',body='$body',show_url='$show_url',message='$message' where id='$id'";
- //echo $sql_edit;
- $result=mysql_query($sql_edit) or die(mysql_error());
- if($result){echo ("<script type='text/javascript'> alert('修改成功!即将返回商品列表!');location.href='list.php';</script>");}
- }
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <title>编辑商品</title>
- </head>
- <LINK href="style.css" type=text/css rel=stylesheet>
- <body><center>
- <?php include(dirname(__FILE__)."/menu.php");?>
- <?php
- $sql="select * from $db_table where id='$id'";
- $query=mysql_query($sql) or die(mysql_error());
- while($rs=mysql_fetch_array($query)){
- ?>
- <form name="form1" method="post" action="edit.php?id=<?=$rs["id"]?>">
- <table width="100%" height="400" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
- <tr align="center">
- <td colspan="2" bgcolor="#999999">【商品修改】</td>
- </tr>
- <tr bgcolor="#CCCCCC">
- <td width="20%" align="center">商品名称:</td>
- <td width="80%"><input name="subject" class=input type="text" value=<?=$rs["subject"]?>></td>
- </tr>
- <tr bgcolor="#CCCCCC">
- <td width="20%" align="center">商品单价:</td>
- <td width="80%"><input name="price" class=input type="text" value=<?=$rs["price"]?> ></td>
- </tr>
- <tr bgcolor="#CCCCCC">
- <td width="20%" align="center">商品描述:</td>
- <td width="80%"><input name="body" class=input type="text" value=<?=$rs["body"]?> ></td>
- </tr>
- <tr bgcolor="#CCCCCC">
- <td width="20%" align="center">展示网址:</td>
- <td width="80%"><input name="show_url" class=input type="text" value=<?=$rs["show_url"]?> ></td>
- </tr>
- <tr bgcolor="#CCCCCC">
- <td width="20%" align="center">详细信息:</td>
- <td width="80%">
- <textarea name="message" type="text" class=input rows="12"><?=$rs["message"]?></textarea>
- </td>
- </tr>
- <tr align="center" bgcolor="#CCCCCC">
- <td colspan="2">
- <input type="submit" name="Submit" value="提交" class="bdtj">
- <input type="reset" name="Submit" value="重置" class="bdtj"></td>
- <input type="hidden" name="id" value="<?=$rs["id"]?>">
- </td>
- </tr>
- </table>
- </form>
- </form>
- <?php
- }
- mysql_close();
- exit;
- ?>
- </center>
- </body>
- </html>
数据查找
- <?php
- include(dirname(__FILE__)."/../config/islogin.php");
- //这里是SESSION来验证用户的合法性
- include(dirname(__FILE__)."/../config/connect.php");
- //数据库连接
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
- <title>商品搜索</title>
- </head>
- <LINK href="style.css" type=text/css rel=stylesheet>
- <style>
- input {width:200px;margin-top:15px;}
- td{height:30px;}
- </style>
- <body><center>
- <?php include(dirname(__FILE__)."/menu.php");?>
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td align="left" bgcolor="#CCCCCC">[搜索结果]</td>
- <td align="right" bgcolor="#CCCCCC">
- <form action='search.php' method=post><input type=text name=q>
- <input type=submit value='搜索商品'>
- </form>
- </td>
- </tr>
- </table>
- <?php
- $q=$_POST["q"];
- if(emptyempty($_POST["q"])){
- echo ("<script type='text/javascript'> alert('请输入关键词!');history.go(-1);</script>");
- exit;
- }
- if ($q!=''){
- $sql="select * from $db_table where id like '%{$q}%' or subject like '%{$q}%' or body like '%{$q}%' or show_url like '%{$q}%' or time like '%{$q}%' or message like '%{$q}%'";
- $res=mysql_query($sql);
- }
- ?>
- <table width="100%" height="51" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF">
- <tr align="center" bgcolor="#999999">
- <td width="5%">ID</td>
- <td width="20%">商品名称</td>
- <td width="5%">单价</td>
- <td width="30%">商品描述</td>
- <td width="10%">展示网址</td>
- <td width="15%">添加时间</td>
- <td width="5%">详情</td>
- </tr>
- <?php
- while($rs=mysql_fetch_array($res)){
- //$res要和前面$res=mysql_query($sql); 一致
- ?>
- <tr class=MouseOut onMouseover="this.className='MouseOver'" onMouseOut="this.className='MouseOut';">
- <td><?=$rs["id"]?></td>
- <td><?=$rs["subject"]?></td>
- <td><?=$rs["price"]?></td>
- <td><?=$rs["body"]?></td>
- <td><?=$rs["show_url"]?></td>
- <td><?=$rs["time"]?></td>
- <td><a href=view.php?id=<?=$rs["id"]?>>【查】</a></td>
- </tr>
- <?php
- }
- mysql_close();
- exit;
- ?>
- </table>
- </center>
- </body>
- </html>
上一篇 css网站模板
下一篇 php字符串编码转换
相关文章