php增刪改查示例自己寫的demo
更新時(shí)間:2013年09月04日 15:11:58 作者:
增刪改查是對(duì)數(shù)據(jù)庫(kù)最基本的操作,下面詳細(xì)為大家介紹下如何連接數(shù)據(jù)庫(kù)以及增刪改查等等相關(guān)知識(shí),感興趣的朋友可以參考下
1.鏈接數(shù)據(jù)庫(kù)通用方法:conn.php
<?php
//第一步:鏈接數(shù)據(jù)庫(kù)
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql鏈接失敗");
//第二步: 選擇指定的數(shù)據(jù)庫(kù),設(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ù)庫(kù)
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ù)庫(kù)<pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫(kù)
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頁(yè)面
<p></p>
<p><br>
</p>
<p></p><pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫(kù)
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.查,列表頁(yè)面<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ù)庫(kù)
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ù)庫(kù)
$conn=@mysql_connect("localhost:3306","root","root")or die ("mysql鏈接失敗");
//第二步: 選擇指定的數(shù)據(jù)庫(kù),設(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ù)庫(kù)
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ù)庫(kù)<pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫(kù)
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頁(yè)面
<p></p>
<p><br>
</p>
<p></p><pre name="code" class="html"><?php
include("conn.php");//引入鏈接數(shù)據(jù)庫(kù)
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.查,列表頁(yè)面<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ù)庫(kù)
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設(shè)計(jì)模式之觀察者模式實(shí)例詳解【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之觀察者模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了php觀察者模式相關(guān)概念、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03PHP Callable強(qiáng)制指定回調(diào)類型的方法
這篇文章主要介紹了PHP Callable強(qiáng)制指定回調(diào)類型的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08Laravel框架運(yùn)行出錯(cuò)提示RuntimeException No application encryption ke
這篇文章主要介紹了Laravel框架運(yùn)行出錯(cuò)提示RuntimeException No application encryption key has been specified.解決方法,涉及Laravel框架相關(guān)配置、設(shè)置與運(yùn)行操作技巧,需要的朋友可以參考下2019-04-04php 遍歷目錄,生成目錄下每個(gè)文件的md5值并寫入到結(jié)果文件中
本文章向大家介紹php遍歷目錄,生成目錄下每個(gè)文件的md5值并寫入到結(jié)果文件中,需要的朋友可以參考下2016-12-12tp5.1 框架數(shù)據(jù)庫(kù)常見(jiàn)操作詳解【添加、刪除、更新、查詢】
這篇文章主要介紹了tp5.1 框架數(shù)據(jù)庫(kù)常見(jiàn)操作,結(jié)合實(shí)例形式詳細(xì)分析了thinkPHP5.1針對(duì)數(shù)據(jù)庫(kù)的添加、刪除、更新、查詢相關(guān)操作技巧與使用注意事項(xiàng),需要的朋友可以參考下2020-05-05