Home > Backend Development > PHP Tutorial > PHP addition, deletion, modification and query example self-written demo_PHP tutorial

PHP addition, deletion, modification and query example self-written demo_PHP tutorial

WBOY
Release: 2016-07-21 16:12:26
Original
768 people have browsed it

1. General method of linking database: conn.php

Copy code The code is as follows:

/ /Step 1: Connect to the database
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql link failed");
//Step 2: Select Specified database, set character set
@mysql_select_db("php_blog",$conn) or die ("db link failed".mysql_error());
mysql_query('SET NAMES UTF8') or die ("Character Set setting error");
?>

2. Add add.php
Copy code The code is as follows:

include("conn.php");//Introducing the linked database
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 "Insertion successful";
}
?>

Title:

Content:


< /form>

3. Delete del.php
Copy code The code is as follows:

include("conn.php");//引入链接数据库
<?php <BR>include("conn.php");//引入链接数据库 <BR>if(!empty ($_GET['id'])){ <BR>$sql="select * from news where id='".$_GET['id']."'"; <BR>$query=mysql_query($sql); <BR>$rs=mysql_fetch_array($query); <BR>} <BR>if(!empty($_POST['sub'])){ <BR>$title=$_POST['title']; <BR>$con=$_POST['con']; <BR>$hid=$_POST['hid']; <BR>$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 "; <BR>mysql_query($sql); <BR>echo "<script> alert('更新成功'); location.href='index.php'</script>"; <br>echo"更新成功"; <br>} <br>?> <br><form action="edit.php" method="post"> <br><input type="hidden" name="hid" value="<?php echo $rs['id']?>"/> <br>标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br> <br>内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br> <br><input type="submit" name="sub" value="发表"> <br></form>


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


 


4,改 edit.php页面





<?php <BR>include("conn.php");//引入链接数据库 <BR>if(!empty ($_GET['id'])){ <BR>$sql="select * from news where id='".$_GET['id']."'"; <BR>$query=mysql_query($sql); <BR>$rs=mysql_fetch_array($query); <BR>} <BR>if(!empty($_POST['sub'])){ <BR>$title=$_POST['title']; <BR>$con=$_POST['con']; <BR>$hid=$_POST['hid']; <BR>$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 "; <BR>mysql_query($sql); <BR>echo "<script> alert('更新成功'); location.href='index.php'</script>"; <br>echo"更新成功"; <br>} <br>?> <br><form action="edit.php" method="post"> <br><input type="hidden" name="hid" value="<?php echo $rs['id']?>"/> <br>标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br> <br>内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br> <br><input type="submit" name="sub" value="发表"> <br></form>


5.查,列表页面
<a href="add.php">添加内容</a> <br><hr> <br><hr> <br><form> <br><input type="text" name="keys" /> <br><input type="submit" name="subs" value="搜索"/> <br></form> <br><?php <BR>include("conn.php");//引入链接数据库 <BR>if(!empty($_GET['keys'])){ <BR>$w=" title like '%".$_GET['keys']."%'"; <BR>}else{ <BR>$w=1; <BR>} <BR>$sql="select * from news where $w order by id desc"; <BR>$query=mysql_query($sql); <BR>while($rs=mysql_fetch_array($query)){ <BR>?> <br><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> <br><li><?php echo $rs['dates'] ?></li> <br><p><?php echo $rs['contents'] ?></p> <br><hr> <br><?php <BR>} <BR>?> <br>






www.bkjia.comtruehttp://www.bkjia.com/PHPjc/313655.htmlTechArticle1.链接数据库通用方法:conn.php 复制代码 代码如下: ?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root","root")or die ("mysql链接失败...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template