search
Homephp教程PHP源码php mysql数据删除,更新,插入保存数据代码

在php入门教程中php mysql是少不了的,下面我们就要告诉各位关于php mysql数据删除,更新,插入保存数据代码下面来一一看实例

<script>ec(2);</script>

一、数据删除

 代码如下 复制代码

//连接数据库教程
$link_id = mysql_connect("localhost","root","") or die("连接失败");
if($link_id)
{
 mysql_select_db("my_test");
 if(!$_GET[id])
 {

  $result=mysql_query("select * from userinfo");
  echo "


    
     
     
     
     
     
     
    ";

 

  while($row=mysql_fetch_array($result)){
   echo "


     
     
     
     
     
     
    ";
  }
  echo "
编号www.111cn.net 用户名称 性别 年龄 注册时间 操作
".$row[id]." ".$row[username]." ".$row[gender]." ".$row[age]." ".$row[regdate]." 删除
";

 }//显示列表的内容
 else
 {

   $sql="delete from userinfo where id=".$_GET[id];
   $result=mysql_query($sql);
   if($result)
    echo "记录已经成功删除
返回";
   else
    echo "记录删除失败
返回";

 }//else($id部分)
} // end ifwww.111cn.net
?>

二、php数据修改

 代码如下 复制代码

//连接数据库
$link_id = mysql_connect("localhost","root","") or die("连接失败");
if($link_id)
{
 mysql_select_db("my_test");
 if(!$_GET[id])
 {

  $result=mysql_query("select * from userinfo");
  echo "


    
     
     
     
     
     
     
    ";

 

  while($row=mysql_fetch_array($result)){
   echo "


     
     
     
     
     
     
    ";
  }
  echo "
编号 用户名称 性别 年龄 注册时间 操作
".$row[id]." ".$row[username]." ".$row[gender]." ".$row[age]." ".$row[regdate]." 编辑www.111cn.net
";

 }//显示列表的内容
 else
 {
  if(!$_POST[ok])
  {
   $sql="select * from userinfo where id=".$_GET[id];
   $result=mysql_query($sql);
   $row=mysql_fetch_array($result);
   ?>
   


   
   echo $row[id]."
"; 
   ?>
   >
   姓名 >

   性别 >

   年龄 >

   注册时间 >

   
   

   
  }// if(!$_POST[ok])
  else{//针对$ok被激活后的处理:
   
   $sql="update userinfo set username='".$_POST[username]."',gender='".$_POST[gender]."',age='".$_POST[age]."',regdate='".$_POST[regdate]."' where id='".$_POST[id]."'";
   $result=mysql_query($sql);
   if($result)
    echo "记录已经成功修改
继续修改记录www.111cn.net";
   else
    echo "记录修改失败
返回";
  }
 }//else($id部分)
} // end if
?>

三、php数据保存

 代码如下 复制代码

if($_POST[ok])
{
 $link_id = mysql_connect("localhost","root","") or die("连接失败");
 if($link_id)
 {
  //选择数据库
  mysql_select_db("my_test");
  //插入数据SQL语句
  $sql="insert into userinfo values('".$_POST[id]."','".$_POST[name]."','".$_POST[gender]."','".$_POST[age]."','".$_POST[regdate]."')";
  //执行SQL语句
  $result=mysql_query($sql);
  if($result)
  {
   echo "记录已经成功插入
继续插入记录";
  }
  else
   echo "执行插入SQL语句失败";
  //关闭数据库
  mysql_close($link_id);
 }
}
else
{
 ?>
 


 编号

 姓名

 性别

 年龄

 注册时间

         
 
 

 
}//end if
?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools