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"插入成功";
  }
?>
<form action="add.php" method="post">
   標題: <input type="text" name="title"><br>
   內容: <textarea rows="5" cols="50" name="con"></textarea><br>
   <input type="submit" name="sub" value="發表">
</form>

3.刪除del.php

<?php
  include("conn.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'</script>";
	echo"更新成功";
  }
?>
<form action="edit.php" method="post">
   <input type="hidden" name="hid" value="<?php echo $rs['id']?>"/>
   標題: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
   內容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
   <input type="submit" name="sub" value="發表">
</form>

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'</script>";
	echo"更新成功";
  }
?>
<form action="edit.php" method="post">
   <input type="hidden" name="hid" value="<?php echo $rs['id']?>"/>
   標題: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
   內容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
   <input type="submit" name="sub" value="發表">
</form>

5.查,列表頁面
<a href="add.php">添加內容</a>
<hr>
<hr>
<form>
     <input type="text" name="keys" />
	 <input type="submit" name="subs" value="搜索"/>
</form>
<?php
  include("conn.php");//引入鏈接數據庫

  if(!empty($_GET['keys'])){
     $w="  title like '%".$_GET['keys']."%'";
  
  }else{
    $w=1;
  }



  $sql="select * from news where $w order by id desc";
  $query=mysql_query($sql);
  while($rs=mysql_fetch_array($query)){
  
  
  
?>

<h2>標題:<a href="view.php?id=<?php echo $rs['id'] ?>"><?php echo $rs['title'] ?></a> <a href="edit.php?id=<?php echo $rs['id'] ?>">編輯</a>||<a href="del.php?del=<?php echo $rs['id'] ?>">刪除</a></h2>
<li><?php echo $rs['dates'] ?></li>
<p><?php echo $rs['contents'] ?></p>

<hr>
<?php
}
?>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章