码迷,mamicode.com
首页 > 数据库 > 详细

使用mysql_query()方法操纵数据库以及综合实例

时间:2015-11-07 19:06:53      阅读:288      评论:0      收藏:0      [点我收藏+]

标签:

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!!!

 

使用mysql_query()方法操纵数据库以及综合实例

标签:

原文地址:http://www.cnblogs.com/xs-yqz/p/4945829.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!