Heim >Backend-Entwicklung >PHP-Tutorial > php增删节查,自己写的demo

php增删节查,自己写的demo

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 12:38:53875Durchsuche

php增删改查,自己写的demo

1.链接数据库通用方法:conn.php

<?php //第一步:链接数据库
    $conn=@mysql_connect("localhost:3306","root","root")or die ("mysql链接失败");
  //第二步: 选择指定的数据库,设置字符集
    @mysql_select_db("php_blog",$conn) or die ("db链接失败".mysql_error());
	mysql_query('SET NAMES UTF8')or die ("字符集设置错误");

?>

2.增加 add.php

<?php include("conn.php");//引入链接数据库
  if(!empty($_POST['sub'])){
	$title=$_POST['title'];
	$con=$_POST['con'];
	echo $sql="insert into news(id,title,dates,contents) value (null,'$title',now(),'$con')" ;
	mysql_query($sql);
	echo"插入成功";
  }
?>
标题:
内容:

3.删除del.php

<?php include("conn.php");//引入链接数据库<pre name="code" class="html"><?php include("conn.php");//引入链接数据库
  if(!empty ($_GET['id'])){
	 $sql="select * from news where id='".$_GET['id']."'";
	 $query=mysql_query($sql);
	 $rs=mysql_fetch_array($query);
	
  
  }

  if(!empty($_POST['sub'])){
	$title=$_POST['title'];
	$con=$_POST['con'];
	$hid=$_POST['hid'];
	$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 ";

	mysql_query($sql);
	echo "<script> alert('更新成功'); location.href='index.php'";
	echo"更新成功";
  }
?>
标题:
内容:

if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "删除成功"; ?>


4,改 edit.php页面


<?php include("conn.php");//引入链接数据库
  if(!empty ($_GET['id'])){
	 $sql="select * from news where id='".$_GET['id']."'";
	 $query=mysql_query($sql);
	 $rs=mysql_fetch_array($query);
	
  
  }

  if(!empty($_POST['sub'])){
	$title=$_POST['title'];
	$con=$_POST['con'];
	$hid=$_POST['hid'];
	$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 ";

	mysql_query($sql);
	echo "<script> alert('更新成功'); location.href='index.php'";
	echo"更新成功";
  }
?>
标题:
内容:

5.查,列表页面
<a href="add.php">添加内容</a>
<hr>
<hr>

标题: 编辑||删除




  • Stellungnahme:
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
    Vorheriger Artikel: 怎的将QVOD与bdhd视频分开 Nächster Artikel: php-GD库函数(3)