php增刪改查示例自己寫的demo
更新時(shí)間:2013年09月04日 15:11:58 作者:
增刪改查是對(duì)數(shù)據(jù)庫最基本的操作,下面詳細(xì)為大家介紹下如何連接數(shù)據(jù)庫以及增刪改查等等相關(guān)知識(shí),感興趣的朋友可以參考下
1.鏈接數(shù)據(jù)庫通用方法:conn.php
<?php
//第一步:鏈接數(shù)據(jù)庫
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql鏈接失敗");
//第二步: 選擇指定的數(shù)據(jù)庫,設(shè)置字符集
@mysql_select_db("php_blog",$conn) or die ("db鏈接失敗".mysql_error());
mysql_query('SET NAMES UTF8')or die ("字符集設(shè)置錯(cuò)誤");
?>
2.增加 add.php
<?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
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"插入成功";
}
?>
<form action="add.php" method="post">
標(biāo)題: <input type="text" name="title"><br>
內(nèi)容: <textarea rows="5" cols="50" name="con"></textarea><br>
<input type="submit" name="sub" value="發(fā)表">
</form>
3.刪除del.php
<?php
include("conn.php");//引入鏈接數(shù)據(jù)庫<pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
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']?>"/>
標(biāo)題: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
內(nèi)容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="發(fā)表">
</form></pre><br>
if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "刪除成功"; ?><p></p>
<pre></pre>
<br>
4,改 edit.php頁面
<p></p>
<p><br>
</p>
<p></p><pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
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']?>"/>
標(biāo)題: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
內(nèi)容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="發(fā)表">
</form></pre><br>
5.查,列表頁面<pre name="code" class="html"><a href="add.php">添加內(nèi)容</a>
<hr>
<hr>
<form>
<input type="text" name="keys" />
<input type="submit" name="subs" value="搜索"/>
</form>
<?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
if(!empty($_GET['keys'])){
$w=" title like '%".$_GET['keys']."%'";
}else{
$w=1;
}
$sql="select * from news where $w order by id desc";
$query=mysql_query($sql);
while($rs=mysql_fetch_array($query)){
?>
<h2>標(biāo)題:<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>
<li><?php echo $rs['dates'] ?></li>
<p><?php echo $rs['contents'] ?></p>
<hr>
<?php
}
?>
</pre><br>
<p></p>
<p><br>
</p>
復(fù)制代碼 代碼如下:
<?php
//第一步:鏈接數(shù)據(jù)庫
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql鏈接失敗");
//第二步: 選擇指定的數(shù)據(jù)庫,設(shè)置字符集
@mysql_select_db("php_blog",$conn) or die ("db鏈接失敗".mysql_error());
mysql_query('SET NAMES UTF8')or die ("字符集設(shè)置錯(cuò)誤");
?>
2.增加 add.php
復(fù)制代碼 代碼如下:
<?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
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"插入成功";
}
?>
<form action="add.php" method="post">
標(biāo)題: <input type="text" name="title"><br>
內(nèi)容: <textarea rows="5" cols="50" name="con"></textarea><br>
<input type="submit" name="sub" value="發(fā)表">
</form>
3.刪除del.php
復(fù)制代碼 代碼如下:
<?php
include("conn.php");//引入鏈接數(shù)據(jù)庫<pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
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']?>"/>
標(biāo)題: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
內(nèi)容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="發(fā)表">
</form></pre><br>
if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "刪除成功"; ?><p></p>
<pre></pre>
<br>
4,改 edit.php頁面
<p></p>
<p><br>
</p>
<p></p><pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
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']?>"/>
標(biāo)題: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
內(nèi)容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="發(fā)表">
</form></pre><br>
5.查,列表頁面<pre name="code" class="html"><a href="add.php">添加內(nèi)容</a>
<hr>
<hr>
<form>
<input type="text" name="keys" />
<input type="submit" name="subs" value="搜索"/>
</form>
<?php
include("conn.php");//引入鏈接數(shù)據(jù)庫
if(!empty($_GET['keys'])){
$w=" title like '%".$_GET['keys']."%'";
}else{
$w=1;
}
$sql="select * from news where $w order by id desc";
$query=mysql_query($sql);
while($rs=mysql_fetch_array($query)){
?>
<h2>標(biāo)題:<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>
<li><?php echo $rs['dates'] ?></li>
<p><?php echo $rs['contents'] ?></p>
<hr>
<?php
}
?>
</pre><br>
<p></p>
<p><br>
</p>
相關(guān)文章
php判斷頁面是否是微信打開的示例(微信打開網(wǎng)頁)
今天遇到一問題,讓一個(gè)頁面在微信上打開,PC上不能直接打開,下面是我使用的方法,現(xiàn)在分享給大家2014-04-04
php無法連接mysql數(shù)據(jù)庫的正確解決方法
這篇文章主要為大家詳細(xì)介紹了php無法連接mysql數(shù)據(jù)庫的正確解決方法,感興趣的小伙伴們可以參考一下2016-07-07
php二維數(shù)組排序與默認(rèn)自然排序的方法介紹
本篇文章介紹了,在php中二維數(shù)組排序與默認(rèn)自然排序的方法。需要的朋友參考下2013-04-04
Laravel 框架控制器 Controller原理與用法實(shí)例分析
這篇文章主要介紹了Laravel 框架控制器 Controller原理與用法,結(jié)合實(shí)例形式分析了Laravel 控制器 Controller基本概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
微信公眾號(hào)開發(fā)之文本消息自動(dòng)回復(fù)php代碼
這篇文章主要為大家詳細(xì)介紹了微信公眾號(hào)開發(fā)之文本消息自動(dòng)回復(fù)php代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
在Mac OS的PHP環(huán)境下安裝配置MemCache的全過程解析
這篇文章主要介紹了在Mac OS的PHP環(huán)境下安裝配置MemCache的全過程解析,MemCache是一套分布式的高速緩存系統(tǒng),需要的朋友可以參考下2016-02-02
php微信公眾平臺(tái)開發(fā)(四)回復(fù)功能開發(fā)
這篇文章主要介紹了php微信公眾平臺(tái)開發(fā)的第四篇,微信回復(fù)功能開發(fā),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
php 實(shí)現(xiàn)301重定向跳轉(zhuǎn)實(shí)例代碼
本文主要介紹php 實(shí)現(xiàn)301重定向跳轉(zhuǎn),通過實(shí)例代碼讓大家更好的理解重定向的方法,有需要的小伙伴可以參考下2016-07-07

