PHP的数据库增删改查源代码

数据|数据库|源代码

//mysql数据库的sql

CREATE TABLE user(  id int(5) DEFAULT '0' NOT NULL AUTO_INCREMENT,  name varchar(20),  city varchar(20),  postcode varchar(10),address varchar(100),  password varchar(10), level tinyint(2) DEFAULT '0',right tinyint(2),email varchar(40), PRIMARY KEY (id),  UNIQUE id (id));
INSERT INTO myuser VALUES (1,'王友','北京','100083','北京市海淀区学院路',111,0,0,'wangyou000@263.net');
INSERT INTO myuser VALUES (2,'金风','武汉','430071','武汉市武昌区水果湖',111,0,0,'jinfeng000@263.net');
INSERT INTO myuser VALUES (3,'常丽','襄樊','441021','襄樊市襄城区南街',111,0,0,'changli000@263.net'); 

1.查寻全部

<html>                                                             
                                                                   
 <head>                                                            
  <title> 查寻全部</title>                                      
 </head>                                                           
                                                                   
<body>                                                             
                                                                   
  <h2 align=center>                                                
  下面是用户数据库的所有内容:                                     
  </h2>                                                            
                                                                   
<?                                                                 
   $db = MySQL_connect("localhost", "root");                       
   MySQL_select_db("mysite",$db);                                  
   $result = MySQL_query("SELECT * FROM user",$db);              
                                                                   
   echo "<table border=1>\n";                                      
   echo "<tr><td>id</td><td>姓名</td>";                            
   echo "<td>城市</td><td>邮编</td>";                              
   echo "<td>住    址</td><td>等级</td>";                          
   echo "<td>权限</td><td>邮件地址</td>";                          
   echo "</tr>\n";                                                 
                                                                   
while ($myrow = MySQL_fetch_row($result))                          
   {                                                               
      printf("<tr><td>%s</td><td>%s</td>", $myrow[0], $myrow[1]);
     printf("<td>%s</td><td>%s</td>", $myrow[2], $myrow[3]);       
     printf("<td>%s</td><td>%s</td>", $myrow[4], $myrow[6]);       
     printf("<td>%s</td><td>%s</td></tr>\n", $myrow[7], $myrow[8]);
   }                                                               
echo "</table>\n";                                                 
?>                                                                 
                                                                   
</body>                                                            
</html>                      

2.根据id查询

<html>                                                           
                                                                 
<body>                                                           
                                                                 
  <title>                                                         
     根据id查询                                               
  </title>                                                       
                                                                 
<?                                                               
$db = MySQL_connect("localhost", "root");                        
MySQL_select_db("mysite",$db);                                   
                                                                 
if ($id) {                                                       
   $result = MySQL_query("SELECT * FROM user WHERE id=$id",$db);
   $myrow = MySQL_fetch_array($result);                          
                                                                 
   printf("<h2 align=left>用户的情况</h2>");                     
   printf("1.ID号 %s\n<br>", $myrow["id"]);                      
   printf("2.姓名: %s\n<br>", $myrow["name"]);                   
   printf("3.城市: %s\n<br>", $myrow["city"]);                   
   printf("4.邮编: %s\n<br>", $myrow["postcode"]);               
   printf("5.住址: %s\n<br>", $myrow["address"]);                
   printf("6.等级:%s\n<br>", $myrow["level"]);                  
   printf("7.邮件地址: %s\n<br>", $myrow["email"]);              
   printf("<a href=\"%s\"> 继续查询 </a>", $PATH_INFO);          
                                                                 
}                                                                
else                                                             
  {                                                              
                                                                 
   $result = MySQL_query("SELECT * FROM user",$db);            
    if ($myrow = MySQL_fetch_array($result))                     
    {                                                            
      printf("<h2 align=left>请选择要查询的用户:</h2>");        
      do {                                                       
        printf("%s<a href=\"%s?id=%s\">%s </a><br>\n",           
        $myrow["id"],$PATH_INFO,$myrow["id"], $myrow["name"]);   
          }                                                      
      while ($myrow = MySQL_fetch_array($result));               
    }                                                            
    else                                                         
    {                                                            
      echo "对不起,没有数据!";                              
    }                                                            
  }                                                              
?>                                                               
                                                                 
</body>                                                          
                                                                 
</html>                                                          

3.更新

<html>                                                          
                                                                
<body>                                                          
                                                                
  <title>                                                        
     更新                                               
  </title>                                                      
                                                                
                                                                
<?                                                              
//PHP程序的开始                                                 
$db = MySQL_connect("localhost", "root");                       
MySQL_select_db("mysite",$db);                                  
                                                                
if (!$id)                                                       
  {                                                             
                                                                
   $result = MySQL_query("SELECT * FROM user",$db);           
    if ($myrow = MySQL_fetch_array($result))                    
    {                                                           
      printf("<h2 align=left>请选择要修改的用户名字:</h2>");   
      do {                                                      
        printf("%s<a href=\"%s?id=%s\">%s </a><br>\n",          
        $myrow["id"],$PATH_INFO,$myrow["id"], $myrow["name"]);  
          }                                                     
      while ($myrow = MySQL_fetch_array($result));              
    }                                                           
    else                                                        
    {                                                           
      echo "对不起!数据库中没有记录";                     
    }                                                           
  }                                                             
else                                                            
{                                                               
  if ($submit)                                                  
   {                                                            
    $sql = "UPDATE user SET name='$name',city='$city',        
    address='$address',email='$email' WHERE id=$id";            
    $result = MySQL_query($sql);                                
    echo "谢谢!数据更改完成\n";                                
   }                                                            
  else                                                          
   {                                                            
    $sql = "SELECT * FROM user WHERE id=$id";                 
    $result = MySQL_query($sql);                             
    $myrow = MySQL_fetch_array($result);                        
//以下不是PHP的部分                                             
?>                                                              
    <h2 align=left>请对用户的信息进行修改:</h2>                
    <form method="post" action="<? echo $PATH_INFO ?>">         
    <input  type=hidden name="id"                               
    value="<?php echo $myrow["id"] ?>">                         
    姓名:<input type="Text" name="first" value="<?php  
   echo $myrow["name"] ?>"><br>                                 
    城市:<input type="Text" name="city" value="<?php echo      
    $myrow["city"] ?>"><br>                                     
    住址:<input type="Text" name="address" value="<?php echo   
    $myrow["address"] ?>"><br>                                  
     电子邮件:<input type="Text" name="email" value="<?php echo
    $myrow["email"] ?>"><br>                                    
     <input type="Submit" name="submit" value="修改完毕">       
    </form>                                                     
<?                                                              
//PHP 的结束                                                    
65:}                                                       
}                                                               
?>                                                              
</body>                                                         
</html>                                                  

4.增加删除记录

<html>                                                          
                                                                    
<body>                                                              
<title>                                                              
   增加删除记录                                                   
</title>                                                            
                                                                    
<?                                                                  
$db = MySQL_connect("localhost", "root");                           
MySQL_select_db("mysite",$db);                                      
                          &nb, sp;                                         
if ($submit)                                                        
   {                                                                
    $sql = "INSERT INTO myuser (name,city,address,email)            
    VALUES ('$name','$city','$address','$email')";                  
    $result = MySQL_query($sql);                                    
     echo "记录添加成功!<p>";                                      
   }                                                                
else                                                                
   if ($delete)                                                     
     {                                                              
      // 删除一条记录                                               
      $sql = "DELETE FROM myuser WHERE id=$id";                 
      $result = MySQL_query($sql);                                  
      echo "记录删除成功!<p>";                                     
     }                                                              
   else                                                             
     {                                                              
    printf("<h2 align=left>删除一个用户的所有信息:</h2>");         
    $result = MySQL_query("SELECT * FROM myuser",$db);              
    while ($myrow = MySQL_fetch_array($result))                     
       {                                                            
          printf("%s:<a href=\"%s?id=%s&delete=yes\">%s</a> <br>\n",
          $myrow["id"],$PATH_INFO, $myrow["id"], $myrow["name"]);   
       }                                                            
  ?>                                                                
                                                                    
  <P>                                                               
                                                                    
  <h2 align=left>                                                   
      添加一个新的用户:                                            
  </h2>                                                             
                                                                    
  <form method="post" action="<?php echo $PATH_INFO?>">             
 姓名:<input type="Text" name="name" value="<?php               
   echo $myrow["name"] ?>"><br>                                     
    城市:<input type="Text" name="city" value="<?php echo          
    $myrow["city"] ?>"><br>                                         
    住址:<input type="Text" name="address" value="<?php echo       
    $myrow["address"] ?>"><br>                                      
     电子邮件:<input type="Text" name="email" value="<?php echo    
    $myrow["email"] ?>"><br>                                        
    <input type="Submit" name="submit" value="添加用户">   
    </form>                                                         
                                                                    
<?php                                                               
}                                                                   
?>                                                                  
                                                                    
</body>                                                             
                                                                    
</html>

时间: 2024-09-24 06:51:53

PHP的数据库增删改查源代码的相关文章

PHP的数据库增删改查源代码(2)

3.更新 <html> <body> <title> 更新 </title> <? //PHP程序的开始 $db = MySQL_connect("localhost", "root"); MySQL_select_db("mysite",$db); if (!$id) { $result = MySQL_query("SELECT * FROM user",$db); i

PHP的数据库增删改查源代码(1)

//mysql数据库的sql CREATE TABLE user( id int(5) DEFAULT '0' NOT NULL AUTO_INCREMENT, name varchar(20), city varchar(20), postcode varchar(10),address varchar(100), password varchar(10), level tinyint(2) DEFAULT '0',right tinyint(2),email varchar(40), PRI

数据库增删改查的代码,有吗

问题描述 数据库增删改查的代码,有吗 用SSH框架如何增删改查数据库的一张学生表,求代码!我已经写到了dao层的实现类. 解决方案 //搜集整理了一下网上的代码.找了半天居然找不到一条插入语句.好郁闷的 //感觉速度还可以.小文件.很多小应用程序在用这个数据库 //SQLite使用办法.直接COPYDLL文件System.Data.SQLite.DLL到应用程序DEBUG目录下. 然后在项目中添加引用,找到这个文件即可 //添加引用 using System;using System.Colle

c#利用webservice和wcf对oracle数据库增删改查,并判断两种方法的效率

问题描述 c#利用webservice和wcf对oracle数据库增删改查,并判断两种方法的效率 初学者,以前没有接触过webservice和wcf.现在遇到一个这样的项目,请大家给予帮助,提供源码,思路都行.当然,最好是代码了,亲,帮一个忙呗~ 解决方案 ws相对简单,WCF就是一把大牛刀,虽然很好,但是你要是杀鸡就得不偿失了.你是了解MVC的话,通信可以试试WebAPI.例子网上到处都是的

ThinkPHP 的模型使用对数据库增删改查(五)

原文:ThinkPHP 的模型使用对数据库增删改查(五) ThinkPHP 的模型使用 // 直接连接数据库,但是得先去配置文件中配置下才行class IndexAction extends Action {public function addDB(){//对应数据库的表 ,一定要对应数据库的表名  大小没关系$user =new Model('Leyangjun');//$user = M('Leyangjun');  //等同上面的new Model() $arr = $user ->se

thinkphp3.2.3版本的数据库增删改查实现代码_php实例

框架thinkphp 版本:3.2.3 内容:数据库操作 1. 多表查找一条数据 M('a表')->join("b表 on b表.id=a表.id")->where('条件')->find(); 2.查找一条数据 M('a表')->where('条件')->find(); 3.多表查询所有数据 M('a表')->join("b表 on b表.id=a表.id")->where('条件')->select(); 4.查询

Node.js操作mysql数据库增删改查_node.js

关于node.js操作mysql数据库的相关介绍请阅读全文吧.下文介绍的非常详细,具体内容如下所示:  安装mysql模块 npm install mysql 数据库准备 mysql server所在的机器IP地址是192.168.0.108,登录账户就用root@123456 在mysql中创建test数据库 在test数据库中创建一张users表 操作 连接数据库 var mysql=require('mysql'); var connection = mysql.createConnect

thinkPHP数据库增删改查操作方法实例详解_php实例

本文实例讲述了thinkPHP数据库增删改查操作方法.分享给大家供大家参考,具体如下: thinkphp对数据库增删改查进行了封装操作,使得使用更加方便,但是不一定灵活. 可以用封装的用,需要写sql,可以执行sql. 1.原始的 $Model = new Model(); // 实例化一个model对象 没有对应任何数据表 $insert_sql = "INSERT INTO sh_wxuser_collection (user_id,store_id,good_id,addtime) VAL

extjs 做数据库增删改查时,原先是全部查上来显示的,现在用条件查询,怎么在原先的位置上显示条件查询的结果

问题描述 extjs 做数据库增删改查时,原先是全部查上来显示的,现在用条件查询,怎么在原先的位置上显示条件查询的结果 问题补充:myali88 写道 解决方案 重写store的onbeforeload方法,构造查询参数,执行查询时这些参数可以直接在后台获取到.这种方式比较好理解.每次刷新(refreshGrid)都会执行这个方法.var grid;/** * grid的参数 */var params = {start : 0,limit : 1000};var rIndex;/** 操作行的i