在php編程中,增刪改查是對數據庫最基本的操做,分享下鏈接數據庫以及增刪改查的一些例子。php
1.連接數據庫通用方法:conn.php mysql
<?php //第一步:連接數據庫 $conn=@mysql_connect("localhost:3306","root","root")or die ("mysql連接失敗"); //第二步: 選擇指定的數據庫,設置字符集 @mysql_select_db("php_blog",$conn) or die ("db連接失敗".mysql_error()); mysql_query('SET NAMES UTF8')or die ("字符集設置錯誤"); ?>
2.增長 add.php
sql
<?php include("conn.php");//引入連接數據庫 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"> 標題: <input type="text" name="title"><br> 內容: <textarea rows="5" cols="50" name="con"></textarea><br> <input type="submit" name="sub" value="發表"> </form>
3.刪除del.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"更新成功"; // www.jbxue.com } ?> <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> if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "刪除成功"; ?><p></p>
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>
5.查,列表頁面ide
<a href="add.php">添加內容</a> <hr> <hr> <form> <input type="text" name="keys" /> <input type="submit" name="subs" value="搜索"/> </form> <?php include("conn.php");//引入連接數據庫 if(!empty($_GET['keys'])){ $w=" title like '%".$_GET['keys']."%'"; }else{ $w=1; } // www.jbxue.com $sql="select * from news where $w order by id desc"; $query=mysql_query($sql); while($rs=mysql_fetch_array($query)){ ?> <h2>標題:<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 } ?>