0

php+mysql数据库简单操作

网站研究 区区第二 2010-05-12 22:23:23

SET NAMES gbk

SET NAMES utf8

 

  1. db_config.php 
  2. <?php 
  3. //数据库连接信息 
  4. $db_host="localhost"
  5. $db_user="root"
  6. $db_pass="vertrigo"
  7. $db_name="test";//数据库名字 
  8. $db_table="pay_list";//数据表名字 
  9. $db_charset="gbk";或utf8,没有- 
  10.  
  11. //登录用户名和密码 
  12. $username="admin"
  13. $password="520"
  14. ?> 
  15.  
  16. connect.php 
  17.  
  18. <?php 
  19. require_once('db_config.php'); 
  20. //连接Mysql失败 
  21. $connect=mysql_connect($db_host,$db_user,$db_passor die("连接Mysql失败!n".mysql_error()); 
  22. //创建数据库 
  23. //mysql_query("create database $db_name") or die("创建数据库失败!n".mysql_error());; 
  24.  
  25. //选择数据库 
  26. mysql_select_db($db_name,$connector die("选择数据库失败!n".mysql_error()); 
  27. //设定字符集 
  28. mysql_query("SET NAMES $db_charset");//设定字符集 
  29.  
  30. ?> 
  31.  
  32. 获取数据 
  33.  
  34. <?php 
  35.  
  36. $sql="select * from $db_table where id='$id'"
  37. $query=mysql_query($sqlor die(mysql_error()); 
  38. while($rs=mysql_fetch_array($query)){ 
  39.  
  40.  
  41.  
  42. //*********** 
  43. //  商品基本信息 
  44.  
  45. /* 商品名称 */ 
  46. $subject$rs["subject"]; 
  47.  
  48. /* 商品描述 */ 
  49. $body   = $rs["body"]; 
  50.  
  51. /* 商品价格(包含运费),以元¥为单位 */ 
  52. $price  = $rs["price"]; 
  53.  
  54.  
  55.   
  56.  
  57.  } 
  58.  
  59.  ?> 

 增加记录

  1. <?php 
  2. include(dirname(__FILE__)."/../config/connect.php"); 
  3. //数据库连接 
  4.  
  5. if (!get_magic_quotes_gpc()) { 
  6. $subject = addslashes($_POST['subject']); 
  7. $price   = addslashes($_POST['price']); 
  8. $body    = addslashes($_POST['body']); 
  9. $show_urladdslashes($_POST['show_url']); 
  10. $message = addslashes($_POST['message']); 
  11.  
  12.  
  13.  
  14.  
  15.  
  16. if(emptyempty($_POST["subject"])||emptyempty($_POST["price"])||emptyempty($_POST["body"])||emptyempty($_POST["show_url"])||emptyempty($_POST["message"])){ 
  17. echo ("<script type='text/javascript'> alert('不能有空值,请检查');history.go(-1);</script>"); 
  18. exit
  19.  
  20.  
  21. //数据库插入记录 
  22. $sql="insert into $db_table (subject,price,body,show_url,time,message) values ('$subject','$price','$body','$show_url',now(),'$message')"
  23.  
  24. $result=mysql_query($sqlor die(mysql_error()); 
  25.  
  26. if($result
  27. {echo ("<script type='text/javascript'> alert('添加成功!即将返回商品列表!');location.href='list.php';</script>");} 

删除记录

 

  1. <?php  
  2. include(dirname(__FILE__)."/../config/islogin.php"); 
  3. //这里是SESSION来验证用户的合法性 
  4.  
  5. include(dirname(__FILE__)."/../config/connect.php"); 
  6. //数据库连接 
  7.  
  8. if($id=$_GET['id'])//获取参数 
  9. $sql="delete from $db_table where id=$id"
  10.  
  11. $delete_result=mysql_query($sqlor die(mysql_error()); 
  12.  
  13. echo ("<script type='text/javascript'> alert('删除成功!即将返回商品列表!');location.href='list.php';</script>"); 
  14.  
  15. else echo ("<script type='text/javascript'> alert('参数错误');history.go(-1);</script>"); 
  16.  
  17. ?> 

 

修改数据

 

  1. <?php  
  2. include(dirname(__FILE__)."/../config/islogin.php"); 
  3. //这里是SESSION来验证用户的合法性 
  4.  
  5. include(dirname(__FILE__)."/../config/connect.php"); 
  6. //数据库连接 
  7.  
  8. $id=$_GET["id"]; 
  9. //获取要修改的ID 
  10. //提交修改 
  11.  
  12.  
  13. if(isset($_POST['Submit'])){ 
  14. /*处理特殊字符*/ 
  15. if (!get_magic_quotes_gpc()) { 
  16. $subject = addslashes($_POST['subject']); 
  17. $price   = addslashes($_POST['price']); 
  18. $body    = addslashes($_POST['body']); 
  19. $show_urladdslashes($_POST['show_url']); 
  20. $message = addslashes($_POST['message']); 
  21. /**/ 
  22. /* 
  23. $subject = $_POST['subject']; 
  24. $price   = $_POST['price']; 
  25. $body    = $_POST['body']; 
  26. $show_url= $_POST['show_url']; 
  27. $message = $_POST['message']; 
  28. */ 
  29. $sql_edit="update  $db_table set subject='$subject',price='$price',body='$body',show_url='$show_url',message='$message' where id='$id'"
  30.  
  31. //echo $sql_edit; 
  32.  
  33. $result=mysql_query($sql_editor die(mysql_error()); 
  34.  
  35. if($result){echo ("<script type='text/javascript'> alert('修改成功!即将返回商品列表!');location.href='list.php';</script>");} 
  36.  
  37. ?> 
  38. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
  39. <html> 
  40. <head> 
  41. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"
  42. <title>编辑商品</title> 
  43. </head> 
  44. <LINK href="style.css" type=text/css rel=stylesheet> 
  45.  
  46. <body><center> 
  47.  
  48. <?php  include(dirname(__FILE__)."/menu.php");?> 
  49.  
  50. <?php 
  51.  
  52.  
  53. $sql="select * from $db_table where id='$id'"
  54. $query=mysql_query($sqlor die(mysql_error()); 
  55.   while($rs=mysql_fetch_array($query)){ 
  56. ?> 
  57.    
  58.   <form name="form1" method="post" action="edit.php?id=<?=$rs["id"]?>"
  59.     <table width="100%" height="400" border="0" cellpadding="0"  cellspacing="1" bgcolor="#000000"
  60.       <tr align="center"
  61.         <td colspan="2" bgcolor="#999999">【商品修改】</td> 
  62.       </tr> 
  63.       <tr bgcolor="#CCCCCC"
  64.         <td width="20%" align="center">商品名称:</td> 
  65.         <td width="80%"><input name="subject" class=input type="text" value=<?=$rs["subject"]?>></td> 
  66.       </tr> 
  67.       <tr bgcolor="#CCCCCC"
  68.         <td width="20%" align="center">商品单价:</td> 
  69.         <td width="80%"><input name="price" class=input type="text" value=<?=$rs["price"]?> ></td> 
  70.       </tr> 
  71.       <tr bgcolor="#CCCCCC"
  72.         <td width="20%" align="center">商品描述:</td> 
  73.         <td width="80%"><input name="body"  class=input type="text" value=<?=$rs["body"]?> ></td> 
  74.       </tr> 
  75.       <tr bgcolor="#CCCCCC"
  76.         <td width="20%" align="center">展示网址:</td> 
  77.         <td width="80%"><input name="show_url"  class=input type="text"  value=<?=$rs["show_url"]?> ></td> 
  78.       </tr> 
  79.       <tr bgcolor="#CCCCCC"
  80.         <td width="20%" align="center">详细信息:</td> 
  81.         <td width="80%"
  82.         <textarea name="message" type="text" class=input rows="12"><?=$rs["message"]?></textarea> 
  83.         </td> 
  84.       </tr> 
  85.                          
  86.       <tr align="center" bgcolor="#CCCCCC"
  87.         <td colspan="2"
  88.         <input type="submit" name="Submit" value="提交" class="bdtj">   
  89.         <input type="reset" name="Submit" value="重置" class="bdtj"></td> 
  90.         <input type="hidden"  name="id" value="<?=$rs["id"]?>"
  91.           </td> 
  92.       </tr> 
  93.     </table> 
  94.   </form>     
  95.      
  96.  
  97.   
  98.   </form>  
  99.   <?php 
  100.  } 
  101.       mysql_close(); 
  102.       exit
  103.       ?> 
  104. </center> 
  105.  
  106. </body> 
  107. </html> 

 

数据查找

 

  1. <?php  
  2. include(dirname(__FILE__)."/../config/islogin.php"); 
  3. //这里是SESSION来验证用户的合法性 
  4.  
  5. include(dirname(__FILE__)."/../config/connect.php"); 
  6. //数据库连接 
  7. ?> 
  8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
  9. <html> 
  10. <head> 
  11. <meta http-equiv="Content-Type" content="text/html; charset=gb2312"
  12. <title>商品搜索</title> 
  13. </head> 
  14. <LINK href="style.css" type=text/css rel=stylesheet> 
  15. <style> 
  16. input {width:200px;margin-top:15px;} 
  17. td{height:30px;} 
  18. </style> 
  19. <body><center> 
  20.   
  21. <?php  include(dirname(__FILE__)."/menu.php");?> 
  22.   
  23.   <table width="100%"  border="0" cellspacing="0" cellpadding="0"
  24.     <tr> 
  25.    <td  align="left" bgcolor="#CCCCCC">[搜索结果]</td> 
  26.    <td  align="right" bgcolor="#CCCCCC"
  27. <form action='search.php' method=post><input type=text name=q> 
  28. <input type=submit value='搜索商品'
  29. </form> 
  30.     </td> 
  31.     </tr> 
  32.   </table> 
  33.    
  34.   <?php  
  35.  
  36. $q=$_POST["q"]; 
  37. if(emptyempty($_POST["q"])){ 
  38. echo ("<script type='text/javascript'> alert('请输入关键词!');history.go(-1);</script>"); 
  39. exit
  40. if ($q!=''){ 
  41. $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}%'"
  42. $res=mysql_query($sql); 
  43.  
  44. ?> 
  45.   <table width="100%" height="51"  border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF"
  46.     <tr align="center" bgcolor="#999999"
  47.       <td width="5%">ID</td> 
  48.       <td width="20%">商品名称</td> 
  49.       <td width="5%">单价</td> 
  50.       <td width="30%">商品描述</td> 
  51.       <td width="10%">展示网址</td> 
  52.       <td width="15%">添加时间</td> 
  53.       <td width="5%">详情</td> 
  54.     </tr> 
  55.     <?php 
  56.     while($rs=mysql_fetch_array($res)){  
  57.     //$res要和前面$res=mysql_query($sql); 一致     
  58.     ?> 
  59.     <tr class=MouseOut onMouseover="this.className='MouseOver'" onMouseOut="this.className='MouseOut';"
  60.       <td><?=$rs["id"]?></td> 
  61.       <td><?=$rs["subject"]?></td> 
  62.       <td><?=$rs["price"]?></td> 
  63.       <td><?=$rs["body"]?></td> 
  64.       <td><?=$rs["show_url"]?></td> 
  65.       <td><?=$rs["time"]?></td> 
  66.       <td><a href=view.php?id=<?=$rs["id"]?>>【查】</a></td> 
  67.     </tr> 
  68.  
  69.     <?php  
  70.         } 
  71.     mysql_close(); 
  72.     exit
  73.  ?> 
  74.   </table> 
  75.  
  76.   </center> 
  77.  
  78. </body> 
  79. </html> 

 

上一篇

相关文章

发表留言