php mysq数据编辑更新实例

 更新时间:2016年11月25日 16:38  点击:1887
本文章是也一款入门级php教程了,主要是告诉你如何利用php mysq数据编辑更新哦。
 代码如下 复制代码

$db = mysql教程_connect("localhost", "phpdb", "phpdb");
mysql_select_db("test",$db);

// 如果提交了submit按钮
if ($submit) {  
   // 如果没有id,则是在增加记录,否则是在修改记录
   if ($id) {
     $sql = "update employees set first='$first',last='$last',     address='$address',position='$position' where id=$id";
   }
   else {
     $sql = "insert into employees (first,last,address,position) values ('$first','$last','$address','$position')";
   }  
   // 向数据库教程发出sql命令
   $result = mysql_query($sql);
   echo "记录修改成功!<>";
   echo "<a href='$php_self'>返回</a>";
}
elseif ($delete) {
 // 删除一条记录
     $sql = "delete from employees where id=$id";
     $result = mysql_query($sql);
     echo "记录删除成功!<>";
  echo "<a href='$php_self'>返回</a>";
}
else {  
   // 如果还没有按submit按钮,那么执行下面这部分程序
   if (!$id) {    
     // 如果不是修改状态,则显示员工列表
     $result = mysql_query("select * from employees",$db);
     while ($myrow = mysql_fetch_array($result)) {
       printf("<a href="%s?id=%s">%s %s</a> ",
        $php_self, $myrow["id"], $myrow["first"], $myrow["last"]);
    printf("<a href="%s?id=%s&delete=yes">(delete)</a><br>",  $php_self, $myrow["id"]);
     }
}
?>
   <a href="<?php echo  $php_self?>">返回</a>
   <form method="post" action="<?php echo  $php_self?>">
   <?php
   if ($id) {
     // 是在编辑修改状态,因些选择一条记录
     $sql = "select * from employees where id=$id";
     $result = mysql_query($sql);
     $myrow = mysql_fetch_array($result);
     $id = $myrow["id"];
     $first = $myrow["first"];
     $last = $myrow["last"];
     $address = $myrow["address"];
     $position = $myrow["position"];
     // 显示id,供用户编辑修改
     ?>
     <input type=hidden name="id" value="<?php echo $id ?>">
     <?php
   }
   ?>
   名:<input type="text" name="first" value="<?php echo $first ?>">
   姓:<input type="text" name="last" value="<?php echo $last ?>">
   <br>
   住址:<input type="text" name="address" value="<?php echo $address ?>">
   职位:<input type="text" name="position" value="<?php echo $position ?>">
   <br>
   <input type="submit" name="submit" value="输入信息">
   </form>
<?php
}
?>
</body>
</html>

 代码如下 复制代码

$qh=mysql_query("select count(*) as rcnt from table where your_condition_here order by whatever");
$data=mysql_fetch_array($qh);
$nr=$data["rcnt"];
//判断偏移量参数是否传递给了脚本,如果没有就使用默认值0

if (empty($offset))
{
$offset=0;
}

//查询结果(这里是每页20条,但你自己完全可以改变它)
$result=mysql_query("select id,name,phone from table where your_condition_here order by whatever limit $offset, 20");

//显示返回的20条记录
while ($data=mysql_fetch_array($result))
{

//换成你用于显示返回记录的代码

}

//下一步,要写出到其它页面的链接
if(!$offset) //如果偏移量是0,不显示前一页的链接
{
$preoffset=$offset-20;
print "<a href="$php_self?offset=$preoffset">前一页</a>&nbsp; ";
}

//计算总共需要的页数
$pages=ceil($nr/20); //$pages变量现在包含所需的页数

for ($i=1; $i <= $pages; $i++)
{
$newoffset=20*$i;
print "<a href="$php_self?offset=$newoffset">$i</a>&nbsp; ";
}

//检查是否是最后一页
if ($pages!=0 && ($newoffset/20)!=$pages)
{
print "<a href="$php_self?offset=$newoffset">下一页</a>&nbsp; ";
}

本教程讲了三个实例都是关于php操作mysql数据库的教程,一是连接mysql数据库,二是查询数据库字段名字,三是查询数据库记录。

//连接mysql数据库

 代码如下 复制代码
$link = mysql_connect("localhost", "mysql_user", "mysql_password")
        or die("could not connect: " . mysql_error());
    print ("connected successfully");
    mysql_close($link);


 
//查询mysql字段名

 代码如下 复制代码

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

$fields = mysql_list_fields("database1", "table1", $link);
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {
    echo mysql_field_name($fields, $i) . " ";
}

//查询数据

 代码如下 复制代码
$conn=mysql_connect("localhost","phpdb","phpdb")
        or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db("test",$conn) or die ("不能选择数据库: ".mysql_error()); 
$result = mysql_query("select * from user",$conn);
while($row=mysql_fetch_array($result)){ 
print "name:".$row[1]; 
print " address:".$row[3]; 
print " tel:".$row[4]; 
print " email:".$row[5];
echo

"<br>";
}

这是一款实用的php教程利用excelfileparser把excel导入数据库教程哦。

 代码如下 复制代码
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.111cn.net/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>excel数据获取演示</title>
<meta name="keywords" content="todo" />
<meta name="description" content="todo"/>
</head>
<body>
<div>
<div>excel数据获取演示</div>
<div>
<form method="post" action="/index/parse" enctype="multipart/form-data">
<input type="file" name="excel" value="" />
<input type="submit" name="submit" value="提交" />
</form>
</div>
</div>
</body>
</html> 

parese.php文件

 

 代码如下 复制代码

<?php
/**
* copyright (c) 2009,
* all rights reserved.
* 文件名:
* 摘 要:
*
* @author 星期八 [url=mailto:ixqbar@hotmail.com]ixqbar@hotmail.com[/url]
* @version
*/

public function parse()
{
/**
* $_files数组说明
* array(n) {
* ["表单文件框名称"] => array(5) {
* ["name"] => 提交文件名称
* ["type"] => 提交文件类型 excel为"application/vnd.ms-excel"
* ["tmp_name"] => 临时文件名称
* ["error"] => 错误(0成功1文件太大超过upload_max_filesize2文件太大超过max_file3上传不完整4没有上传文件)
* ["size"] => 文件大小(单位:kb)
* }
* }
*/
$return=array(0,'');
/**
* 判断是否提交
* is_uploaded_file(文件名称)用于确定指定的文件是否使用post方法上传,防止非法提交,通常和move_upload_file一起使用保存上传文件到指定的路径
*/
if(!isset($_files) || !is_uploaded_file($_files['excel']['tmp_name']))
{
$return=array(1,'提交不合法');
}
//处理
if(0 == $return[0])
{
import('@.util.excelparser');
$excel=new excelparser($_files['excel']['tmp_name']);
$return=$excel->main();
}
//输出处理
print_r($return);
?>

 代码如下 复制代码
* 1、连接服务器 2、各类sql动作
*/
class mysql{
    private $host;  //服务器地址
    private $name;  //用户名称
    private $pass;  //密码
    private $table; //连接数据库教程
    private $jiema; //设置解码
    private $ztime; //设置服务器的时区
    //构造函数
    function __construct($host,$name,$pass,$table,$jiema,$ztime){
        $this -> host  = $host  ;
      $this -> name  = $name  ;
      $this -> pass  = $pass  ;
      $this -> table = $table ;
  $this -> jiema = $jiema ;
  $this -> ztime = $ztime ;
      $this -> connect();
     }
     //数据库连接和设置
     function connect(){
         $link=@mysql_connect($this->host,$this->name,$this->pass) or die ("连接服务器失败");
         @mysql_select_db($this->table,$link) or die("连接数据失败");
         @mysql_query("set names '$this->jiema'");
      @date_default_timezone_set("$this->ztime");
     }
    //执行操作
 function query($sql) {
     if(!($query = @mysql_query($sql))) $this->show($sql);
  return $query;
 }
    //显示信息
    function show($message = '', $sql = '') {
  if(!$sql) echo $message;
  else echo $message.'<br>'.$sql;
 }
    //取得数据集的某个值
 function result($query,$row,$values) {
  return @mysql_result($query,$row,$values);
 }
    //取得数据集的某个值
 function get_values($table,$row,$values) {
     $query = $this -> query("select * from $table");
  $returnvalues = mysql_result($query,$row,$values);
  return $returnvalues;
 }
    //取得数据集的行数
 function num_rows($query) {
  return @mysql_num_rows($query);
 }
    //循环读取数据
 function fetch($query) {
  return @mysql_fetch_array($query);
 }
    //最后一次插入纪录的id值
 function insert_id() {
  return mysql_insert_id();
 }
    //取得数据集中的一行
 function fetch_row($query) {
  return mysql_fetch_row($query);
 }
    //插入一条数据
    function fn_insert($table,$name,$value){
     if($this->query("insert into $table ($name) values ($value)")){
      return true;
  }else{
      return false;
  }
    }
    //插入任意数据
    function sql_insert($tbname,$postvalues){
        foreach ($postvalues as $key => $value) {
   $postvalue .= "`".$key."`".",";
   $sqlvalue .= "'".$value."',";
  }
  $sqlfield = mb_substr("$postvalue",0,-1,'gbk');
  $sqlvalue = mb_substr("$sqlvalue",0,-1,'gbk');
  if($this-> fn_insert("$tbname","$sqlfield","$sqlvalue")){
      return true;
  }else{
      return false;
  }
 } 
    //修改万能数据
    function sql_update($table,$postvalues,$wwhere){
  foreach ($postvalues as $key=>$value) {
   $sqlfield .= $key."="."'".$value."'".",";
  }
  $sqlfield= mb_substr("$sqlfield",0,-1,'gbk');
  if($this->fn_update("$table","$sqlfield","$wwhere")){
      return true;
  }else{
      return false;
  }
 }
    //修改一条数据
    function fn_update($table,$value,$wwhere){
     if($this->query("update $table set $value where $wwhere")){
      return true;
     }else{
      return false;
  }
    }
    //删除一条数据
    function sql_delete($table,$wwhere){
     if($this->query("delete from $table where $wwhere")){
      return true;
  }else{
      return false;
  }
 }
 //关闭数据连接
    function close() {
  return mysql_close();
 }
}
$db =  new mysql($location['host'],$location['hostname'],$location['hostpass'],$location['table'],$location['jiema'],$location['ztime']);   
?>
[!--infotagslink--]

相关文章