php增删改查demo

2025-10-22 22:57:06

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

代码如下:

<?php 

include("conn.php");//引入链接数据库<pre name="code" class="html"><?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></pre><br> 

<?php

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

<pre></pre> 

<br> 

php增删改查demo

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></pre><br> 

php增删改查demo

5、查询select.php页面

代码如下:

<?php 

include("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 "{$array['title']}$neirong<a href='edit.php?id=$id'>编辑</a>&nbsp;&nbsp;

<a href='delete.php?id=$id'>删除</a><hr />";

}

}

?>

php增删改查demo

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢