使用mysql_query()方法操縱數據庫以及綜合實例


1.利用insert 語句添加記錄

<? require('conn.php');        
mysql_query( "insert into lyb ( title, content, author, email,`date`) values ('大家好', 'PHP學習園地', '小浣熊', 'sdf@sd.com','2012-3-3')") or die('執行失敗');
echo '新增記錄的id是'.mysql_insert_id();        //可選,輸出新記錄的id
?>

2.利用delete語句刪除數據

<?    require('conn.php');    
mysql_query( " Delete from lyb where ID in(158,162,163,169)") or die('執行失敗');
?>
本次操作共有<?= mysql_affected_rows() ?>條記錄被刪除!

3.利用updata語句更新數據

<?    require('conn.php');        
mysql_query("Update lyb set email='rong@163.com', author='蓉蓉' where ID>133 and ID<143") or die('執行失敗');
?>

 接下來演示一個完整的例子

5.1.php為插入頁面

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 13:50:48
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8");  

 if (isset($_POST['submit'])) {
     require("include/conn.php");
 mysql_select_db("lyb",$conn);//選擇數據庫

 $title = $_POST['title'];
 $author = $_POST['author'];
 $content = $_POST['content'];
 $email = $_POST['email'];
 $result = mysql_query("insert into `lyb1`(`id`,`title`,`content`,`author`,`email`) values(null,'$title','$content','$author','$email')")or die('執行失敗');;
 var_dump($result);
 echo "新增的記錄的id為".mysql_insert_id(); 
 mysql_free_result($result);
 mysql_close($result);
 echo "<script>alert('插入完成');</script>";
 header("Location:5.6.php");
/* header("refresh:3;url=5.6.php");*/ //3秒后 頁面跳轉
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加數據頁面</title>
</head>
<body>
    <form action="5.1.php" method="POST">
        <p>添加新聞頁面</p>
        <div>標題:<input type="text" name="title" id=""></div>
        <div>內容: <textarea  name="content" id="" cols="30" rows="5"></textarea></div>
        <div>作者:<input type="text" name="author" id=""></div>
        <div>郵箱:<input type="text" name="email" id=""></div>
        <div><input type="reset" value="重置"><input type="submit" name="submit" value="提交"></div>
    </form>
</body>
</html>

 

5.6.php為從數據庫后台提取數據 顯示在前台的頁面

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 15:10:04
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8"); 
 require("include/conn.php");
 mysql_select_db("lyb",$conn);//選擇數據庫
 $result = mysql_query("select * from `lyb1`",$conn);//選擇數據庫表
 ?>
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Document</title>
 </head>
 <body>
     <a href="5.1.php">添加記錄</a>
     <table border="1">
         <tr bgcolor="#ccc">
             <th>序號</th>
             <th>標題</th>
             <th>內容</th>
             <th>作者</th>
             <th>郵箱</th>
             <th>刪除</th>
             <th>更新</th>
         </tr>
         <?php
         while ($row = mysql_fetch_assoc($result)) {
         ?>
             <tr>
                 <td><?= $row['id']?></td>
                 <td><?= $row['title']?></td>
                 <td><?= $row['content']?></td>
                 <td><?= $row['author']?></td>
                 <td><?= $row['email']?></td>
                 <td><a href="delete.php?id=<?= $row['id']?>">刪除</a></td>
                 <td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
             </tr>
         <?php        
         }
         ?>
     </table>
     <p>共有<?= mysql_num_rows($result) ?>條記錄 </p>
     <!-- mysql_num_rows()函數返回的是結果集的總數 -->
<?php //釋放資源,關閉結果集 mysql_free_result($result); mysql_close($result); ?> </body> </html>

delete.php刪除程序

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 15:26:27
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8"); 
 require("include/conn.php");
 mysql_select_db("lyb",$conn);//選擇數據庫
 $id = intval($_GET['id']);//獲取5-6.php傳來的id參數並轉換為整型
 $sql = "delete from `lyb1` where `id` = $id ";//刪除指定的內容
 if (mysql_query($sql) && mysql_affected_rows()==1) {//執行sql語句並判斷執行是否成功
     echo "<script>alert('刪除成功!');location.href='5.6.php'</script>";
 }else{
     echo "<script>alert('刪除失敗!');location.href='5.6.php'</script>";
 }
?>

多選刪除頁面deleteall.php

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 15:10:04
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8"); 
 require("include/conn.php");
 mysql_select_db("lyb",$conn);//選擇數據庫

 if ($_GET["del"]==1) {//如果用戶按了“刪除”按鈕
     $selectid=$_POST["selected"];//獲取所有選中多選框的值,保存到數組中
     if( count($selectid)>0){//防止selectid值為空時執行SQL語句出錯
         $sel = implode(",", $selectid);//將各個數組元素用“,”號連接起來
         mysql_query("delete from `lyb1` where `id` in($sel)")or die("執行失敗".mysql_error());
         header("Location:deleteall.php");//刪除完畢,刷新頁面
     }else{
         echo "沒有被選中的數據";
     }
 }

 $result = mysql_query("select * from `lyb1`",$conn);//選擇數據庫表
 ?>
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>Document</title>
 </head>
 <body>
     <form method="post" action="?del=1"> <!--表單提交給自身-->
     <a href="5.1.php">添加記錄</a>
     <table border="1">
         <tr bgcolor="#ccc">
             <th>序號</th>
             <th>標題</th>
             <th>內容</th>
             <th>作者</th>
             <th>郵箱</th>
             <th>刪除</th>
             <th>更新</th>
         </tr>
         <?php
         while ($row = mysql_fetch_assoc($result)) {
         ?>
             <tr>
                 <td><?= $row['id']?></td>
                 <td><?= $row['title']?></td>
                 <td><?= $row['content']?></td>
                 <td><?= $row['author']?></td>
                 <td><?= $row['email']?></td>
                 <td><input type="checkbox" name="selected[]" id="" value="<?= $row['id']?>"></td><!-- 刪除的復選框 -->
                 <td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
             </tr>
         <?php        
         }
         ?>
         <tr bgcolor="#ddd">
             <td></td>
             <td></td>
             <td></td>
             <td></td>
             <td></td>
             <td><input type="submit" value="刪 除"></td><!-- 刪除按鈕 -->
             <td></td>
         </tr>
     </table>
     <p>共有<?= mysql_num_rows($result) ?>條記錄 </p>
     </form>
     <!-- mysql_num_rows()函數返回的是結果集的總數 -->
     <?php
     //釋放資源,關閉結果集
     mysql_free_result($result);
     mysql_close($result);
     ?>

 </body>
 </html>

更新頁面的代碼 editform.php

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 16:39:42
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8"); 
 require("include/conn.php");
 mysql_select_db("lyb",$conn);//選擇數據庫


$id = intval($_GET['id']);//將獲取的id強制轉換為整型
$sql = "select * from `lyb1` where id = $id";
echo $sql;
$result = mysql_query($sql,$conn);//選擇數據庫表
$row = mysql_fetch_assoc($result);//將待更新記錄各字段的值存入數組中


 if ($_POST["submit"]) {//當單擊確認按鈕后執行更新語句
     $title = $_POST['title'];
     $author = $_POST['author'];
     $content = $_POST['content'];
     $email = $_POST['email'];
     $sql_del = "Update `lyb1` Set title='$title',author='$author',email='$email',content='$content' Where id='$id'";
     echo $sql_del;
     mysql_query($sql_del)or die("執行失敗".mysql_error());
     echo "<script>alert('留言修改成功');location.href='5.6.php'</script>";
 }


?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加數據頁面</title>
</head>
<body>
    <form action="?id=<?= $row['id']?>" method="POST">
        <p>添加新聞頁面</p>
        <div>標題:<input type="text" name="title" id="" value="<?= $row['title']?>"></div>
        <div>內容:    <textarea  name="content" id="" cols="30" rows="5"><?= $row['content']?></textarea></div>
        <div>作者:<input type="text" name="author" id="" value="<?= $row['author']?>"></div>
        <div>郵箱:<input type="text" name="email" id="" value="<?= $row['email']?>"></div>
        <div><input type="reset" value="重置"><input type="submit" name="submit" value="確定"></div>
    </form>
</body>
</html>

查詢記錄的實現search.php

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 17:31:16
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8"); 
 require("include/conn.php");
 mysql_select_db("lyb",$conn);//選擇數據庫
 $result = mysql_query("select * from `lyb1`",$conn);
 ?>
 <!DOCTYPE html>
 <html lang="en">
 <head>
     <meta charset="UTF-8">
     <title>查詢頁面</title>
 </head>
 <body>
 <form action="search_result.php" method="get">
         <div style="border:1px solid gray; background:#eee;padding:4px;"> 
             查找留言:請輸入關鍵字 <input name="keyword" type="text">
             <select name="sel">
                 <option value="title">文章標題</option>
                 <option value="content">文章內容</option>
             </select>
             <input type="submit" name="submit" value="查詢">
         </div>
     </form>

     <table border="1">
             <tr bgcolor="#ccc">
                 <th>序號</th>
                 <th>標題</th>
                 <th>內容</th>
                 <th>作者</th>
                 <th>郵箱</th>
                 <th>刪除</th>
                 <th>更新</th>
             </tr>
             <?php
             while ($row = mysql_fetch_assoc($result)) {
                 ?>
                 <tr>
                     <td><?= $row['id']?></td>
                     <td><?= $row['title']?></td>
                     <td><?= $row['content']?></td>
                     <td><?= $row['author']?></td>
                     <td><?= $row['email']?></td>
                     <td><a href="delete.php?id=<?= $row['id']?>">刪除</a></td>
                     <td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
                 </tr>
                 <?php
             }
             ?>
             </table>
             <p>共有<?= mysql_num_rows($result) ?>條記錄</p>
 </body>
 </html>

查詢結果的顯示頁面search_result.php

<?php
/*
 * 
 * @Authors peng--jun 
 * @Email   1098325951@qq.com
 * @Date    2015-11-07 17:40:21
 * @Link    http://www.cnblogs.com/xs-yqz/
 * @version $Id$
 ==========================================
 */
 header("Content-type: text/html; charset=UTF-8"); 
 require("include/conn.php");
 mysql_select_db("lyb",$conn);//選擇數據庫
     $keyword=trim($_GET['keyword']);//獲取輸入的關鍵字
     $sel=$_GET['sel'];//獲取選擇的查詢類型
     $sql="select * from `lyb1`";
     if ($keyword<>"") {
         $sql=$sql ." where $sel like '%$keyword%'";    //構造查詢語句
     }
     $result=mysql_query($sql) or die('執行失敗');
     if (mysql_num_rows($result)>0) {
         echo "<p>關鍵字為“ $keyword ”,共找到".mysql_num_rows($result)." 條留言</p>"; 
 
     ?>
     <!DOCTYPE html>
     <html lang="en">
     <head>
         <meta charset="UTF-8">
         <title>查詢結果</title>
     </head>
     <body>

         <table border="1">
             <tr bgcolor="#ccc">
                 <th>序號</th>
                 <th>標題</th>
                 <th>內容</th>
                 <th>作者</th>
                 <th>郵箱</th>
                 <th>刪除</th>
                 <th>更新</th>
             </tr>
             <?php
             while ($row = mysql_fetch_assoc($result)) {
                 ?>
                 <tr>
                     <td><?= $row['id']?></td>
                     <td><?= $row['title']?></td>
                     <td><?= $row['content']?></td>
                     <td><?= $row['author']?></td>
                     <td><?= $row['email']?></td>
                     <td><a href="delete.php?id=<?= $row['id']?>">刪除</a></td>
                     <td><a href="editform.php?id=<?= $row['id']?>">更新</a></td>
                 </tr>
                 <?php        
             }
         }else echo "沒有搜索到任何留言";
             ?>
         </table>
 </body>
 </html>

 

 

至此,整個所有的操作都完成了。over!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM