php增删改查demo

时间:2024-10-14 01:05:34

1、链接数据库方法:conn.php 。代码如下:<?php$link=mysql_connect('localhost','root','tianqing')or die('数据库连接错误');mysql_select_db('studyexam',$link)or die('数据库访问错误');mysql_query("set names 'utf8'");?>

php增删改查demo

2、增加 add.php代码如下:<?php include("conn.php");//引入链接数据库 if(!empty($_POST['tj'])){ $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 /><br />内容: <textarea rows="6" cols="55" name="con"></textarea><hr><input type="submit" name="tj" value="提交"></form>

php增删改查demo

3、删除del.php代码如下:<?phpinclude("conn.php");//引入链接数据库<pre name="code" class="html"><?phpinclude("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></pre><br><?phpif(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "删除成功"; ?><p></p><pre></pre><br>

php增删改查demo

4、改 edit.php页面代码如下:<?phpinclude("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></pre><br>

php增删改查demo

5、查询select.php页面代码如下:<?phpinclude("conn.php");$sql="select * from entries";//entries这是数据库名词,大家自己改if($result=mysql_query($sql)){ /* $num=mysql_num_rows($result) for($i=1;$i<=$num;$i++); */ while($array=mysql_fetch_assoc($result)){ //$biaoti=$array['title']; $neirong=$array['entry']; $id=$array['entry_id']; echo "<p>{$array['title']}</p><p>$neirong</p><p><a href='edit.php?id=$id'>编辑</a>&nbsp;&nbsp; <a href='delete.php?id=$id'>删除</a></p><hr />"; }}?>

php增删改查demo
© 手抄报圈