当前位置:Gxlcms > PHP教程 > php增删改查示例自己写的demo_PHP教程

php增删改查示例自己写的demo_PHP教程

时间:2021-07-01 10:21:17 帮助过:30人阅读

1.链接数据库通用方法:conn.php
代码如下:
  1. <br><!--?php <BR-->//第一步:链接数据库 <br>$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql链接失败"); <br>//第二步: 选择指定的数据库,设置字符集 <br>@mysql_select_db("php_blog",$conn) or die ("db链接失败".mysql_error()); <br>mysql_query('SET NAMES UTF8')or die ("字符集设置错误"); <br>?> <br> <br>2.增加 add.php <br><span style="CURSOR: pointer" onclick="doCopy('code96652')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->include("conn.php");//引入链接数据库 <br>if(!empty($_POST['sub'])){ <br>$title=$_POST['title']; <br>$con=$_POST['con']; <br>echo $sql="insert into news(id,title,dates,contents) value (null,'$title',now(),'$con')" ; <br>mysql_query($sql); <br>echo"插入成功"; <br>} <br>?> <br> <br> <br>3.删除del.php <br><span style="CURSOR: pointer" onclick="doCopy('code74626')"><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><!--?php <BR-->include("conn.php");//引入链接数据库<pre name="code" class="html layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><!--?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 ""; <br>echo"更新成功"; <br>} <br>?> <br></li></ol></pre><br> <br>if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "删除成功"; ?><p></p> <br><pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li></li></ol></pre> <br><br> <br>4,改 edit.php页面 <br><p></p> <br><p><br> <br></p> <br><p></p><pre name="code" class="html layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><!--?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 ""; <br>echo"更新成功"; <br>} <br>?> <br></li></ol></pre><br> <br>5.查,列表页面<pre name="code" class="html layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>添加内容 <br><hr> <br><hr> <br> <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>标题:"><!--?php echo $rs['title'] ?--> ">编辑||">删除</h2> <br><li><!--?php echo $rs['dates'] ?--></li> <br><p><!--?php echo $rs['contents'] ?--></p> <br><hr> <br><!--?php <BR-->} <br>?> <br></li></ol></pre><br> <br><p></p> <br><p><br> <br></p> <br></li><li><p></p></li><li><p align="left"><span id="url" itemprop="url">http://www.bkjia.com/PHPjc/313655.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http://www.bkjia.com/PHPjc/313655.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">1.链接数据库通用方法:conn.php 代码如下:</span></p><pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li>?php //第一步:链接数据库 $conn=@mysql_connect("localhost:3306","root","root")or die ("mysql链接失败...<p></p></li><li> </li></ol></pre></li></ol></pre></li></ol></pre>

人气教程排行