Home >Backend Development >PHP Tutorial >PHP addition, deletion, modification and query example self-written demo_PHP tutorial
1. General method of linking database: 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>
<?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>
<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>