MySQL函数操作数据库

1.select语句查询信息(实现模糊查询)

<form name="form1" method="post" action="">
    <input name="txt_keyword" type="text" id="txt_keyword" size="40">
    <input name="Submit" type="submit" class="btn_grey" value="搜索" onclick="return check(form)">
</form>
<script type="text/javascript">
    function check(form){
        if(form.txt_keyword.value==""){
            alert("请输入查找关键字!");
            form.txt_keyword.focus();
            return false;
        }
        form.submit(); //提交表单
    }
</script>
<?php
    $conn=mysql_connect("localhost","root","root")or die("数据库连接错误".mysql_error()); //连接数据库
    mysql_select_db("testphp",$conn)or die("数据库访问错误".mysql_error());  //选择数据库
    mysql_query("set names 'utf8'");         //选择编码格式
    $keyword=$_POST[txt_keyword];            //获取输入的关键词
    $sql=mysql_query("select * from tb_affiche where title like '%$keyword%' or content like '%$keyword%'"); //执行查询语句
    $row=mysql_fetch_object($sql); //获取查询结果集
    if(!$row){         //判断查询结果集是否存在,不存在返回true
        echo "<font color='red'>你搜索的信息不存在!</font>";
    }
    do{                                   //do...while输出查询结果
        ?>
        <table border="1">
        <tr bgcolor="#fffff">
            <td width="50" height="31"><?php echo $row->title;?></td>
            <td width="433" height="31"><?php echo $row->content;?></td>
        </tr>
        </table>
        <?php
    }while ($row=mysql_fetch_object($sql)); 
    mysql_free_result($sql);     //关闭记录集
    mysql_close($conn);      // 关闭数据库连接
?>

2.insert语句添加信息

add_affiche.php页面的代码
<form name="form1" method="post" action="check_add_affiche.php">
    <table width="520" height="212" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
        <tr>
            <td width="87" align="center">公告主题:</td>
            <td width="433" height="31">
                <input name="txt_title" type="text" id="txt_title" size="40">*
            </td>
        </tr>
        <tr>
            <td width="124" align="center">公告内容:</td>
            <td>
                <textarea name="txt_content" cols="50" rows="8" id="txt_content"></textarea>
            </td>
        </tr>
        <tr>
            <td height="40" colspan="2" align="center">
                <input name="Submit" type="submit" class="btn_grey" value="保存" onclick="return check(form)">
                <input name="Submit2" type="reset" value="重置">
            </td>
        </tr>
    </table>
</form>
<script type="text/javascript">
    function check(form){
        if(form.txt_title.value==""){
            alert("请输入公告标题!");
            form.txt_title.focus();
            return false;
        }
        if(form.txt_content.value==""){
            alert("请输入公告内容!");
            form.txt_content.focus();
            return false;
        }
        form.submit();
    }
</script>

 

check_add_affiche.php页面代码
<?php
    $conn=mysql_connect("localhost","root","root")or die("数据库连接错误".mysql_error());
    mysql_select_db("testphp",$conn)or die("数据库访问错误".mysql_error());
    mysql_query("set names 'utf8'");
    $title=$_POST[txt_title];
    $content=$_POST[txt_content];
    $createtime=date("Y-m-d H:i:s");
    $sql=mysql_query("insert into tb_affiche(title,content,createtime)values('$title','$content','$createtime')");
    echo "<script>alert('公告信息添加成功!');window.location.href='add_affiche.php';</script>";
    mysql_free_result($sql);
    mysql_close($conn);
?>

 

3.update语句修改信息

<?php
    $conn=mysql_connect("localhost","root","root")or die("数据库连接错误".mysql_error());
    mysql_select_db("testphp",$conn)or die("数据库访问错误".mysql_error());
    mysql_query("set names 'utf8'");
    $title=$_POST[txt_title];
    $content=$_POST[txt_content];
    $id=$_POST[id];
    $sql=mysql_query("update tb_affiche set title='$title',content='$content'where id=$id");
    if($sql){
        echo "<script>alert('公告信息编辑成功!');history,back();window.location.href='modify.php?id=$id';</script>";
    }
    else{
        echo "<script>alert('公告信息编辑失败!');history,back();window.location.href='modify.php?id=$id';</script>";
    }
    mysql_free_result($sql);
    mysql_close($conn);
?>

 

4.delete语句删除信息

<?php
    $conn=mysql_connect("localhost","root","root")or die("数据库连接错误".mysql_error());
    mysql_select_db("testphp",$conn)or die("数据库访问错误".mysql_error());
    mysql_query("set names 'utf8'");
    $id=$_POST[id];
    $sql=mysql_query("delete from tb_addiche where id=$id");
    if($sql){
        echo "<script>alert('公告信息删除成功!');history,back();window.location.href='delete_affiche.php?id=$id';</script>";
    }
    else{
        echo "<script>alert('公告信息删除失败!');history,back();window.location.href='delete_affiche.php?id=$id';</script>";
    }
    mysql_free_result($sql);
    mysql_close($conn);
?>
posted @ 2016-09-02 15:14  mengyin  阅读(307)  评论(0编辑  收藏  举报