亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP開發(fā)新聞管理系統(tǒng)之添加功能的實(shí)現(xiàn)

修改功能的實(shí)現(xiàn),我們來看以下流程圖

adds.png

下面我們來看以下添加頁的代碼:news.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <style type="text/css">
    *{margin:0px;padding:0px;}
    body{background:#ccc;}
    .add{width:450px;height:280px;background:#eee;float:left;}
    .cont{width:500px;height:350px;margin-top:5px;margin-left:5px;}
    form{margin-left:10px;padding-top:30px;}
    .sub{width:100px;height:40px;border:1px solid #ccc;}
    .sub:hover{background:#f90}
    </style>
</head>
<body>
    <div class="add">
        <div class="cont">
            <form method="post" action="addnews.php">
                標(biāo)題:<input type="text" name="title"></br></br>
                內(nèi)容:<textarea cols="50" rows="5" name="content"></textarea></br></br>
                <input type="submit" value="添加" class="sub">
            </form>
        </div>
    </div>
</body>
</html>

如上代碼可以看到,表單提交到addnews.php文件中

下面我們來看以下addnews.php文件的代碼:

首先我們要連接數(shù)據(jù)庫,添加,是從表單獲取信息添加到數(shù)據(jù)庫中,所以我們要連接數(shù)據(jù)庫

代碼如下:

????header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
?? ?$con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫連接失敗");
?? ?mysql_select_db('news') or die("指定的數(shù)據(jù)庫不能打開");
?? ?mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫的字符集

然后獲取表單信息:

????$title = $_POST['title'];
?? ?$content = $_POST['content'];
?? ?$messtime = time();

在對(duì)數(shù)據(jù)庫添加之前,我們先要判斷一下,文本框的標(biāo)題和內(nèi)容是不是為空,如果為空,我們給出提示,代碼如下:

????if(empty($title)){
?? ??? ?echo "<script>alert('請(qǐng)輸入標(biāo)題');history.go(-1);</script>";
?? ?}elseif(empty($content)){
?? ??? ?echo "<script>alert('請(qǐng)輸入內(nèi)容');history.go(-1);</script>";
?? ?}

當(dāng)內(nèi)容不為空時(shí),我們才可以往數(shù)據(jù)庫添加內(nèi)容,代碼如下:

????????$sql = "insert into new (title,content,messtime) values('$title','$content','$messtime')";
?? ??? ?$result =mysql_query($sql);
?? ??? ?if($result){
?? ??? ??? ?echo "<script>alert('添加文章成功');location.href='newlist.php'</script>";
?? ??? ?}else{
?? ??? ??? ?echo "<script>alert('添加文章失敗');history.go(-1);</script>";
?? ??? ?}


完整源碼如下:

<?php
    //鏈接數(shù)據(jù)庫
    header("Content-type: text/html; charset=utf-8");//設(shè)置編碼
    $con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫連接失敗");
    mysql_select_db('news') or die("指定的數(shù)據(jù)庫不能打開");
    mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫的字符集

    //添加操作
    $title = $_POST['title'];
    $content = $_POST['content'];
    $messtime = time();

    if(empty($title)){
        echo "<script>alert('請(qǐng)輸入標(biāo)題');history.go(-1);</script>";
    }elseif(empty($content)){
        echo "<script>alert('請(qǐng)輸入內(nèi)容');history.go(-1);</script>";
    }else{
        $sql = "insert into new (title,content,messtime) values('$title','$content','$messtime')";
        $result =mysql_query($sql);
        if($result){
            echo "<script>alert('添加文章成功');location.href='newlist.php'</script>";
        }else{
            echo "<script>alert('添加文章失敗');history.go(-1);</script>";
        }
    }
?>
繼續(xù)學(xué)習(xí)
||
<?php //鏈接數(shù)據(jù)庫 header("Content-type: text/html; charset=utf-8");//設(shè)置編碼 $con =@mysql_connect("localhost","root","root") or die("數(shù)據(jù)庫連接失敗"); mysql_select_db('news') or die("指定的數(shù)據(jù)庫不能打開"); mysql_query("set names utf8");//設(shè)置數(shù)據(jù)庫的字符集 //添加操作 $title = $_POST['title']; $content = $_POST['content']; $messtime = time(); if(empty($title)){ echo "<script>alert('請(qǐng)輸入標(biāo)題');history.go(-1);</script>"; }elseif(empty($content)){ echo "<script>alert('請(qǐng)輸入內(nèi)容');history.go(-1);</script>"; }else{ $sql = "insert into new (title,content,messtime) values('$title','$content','$messtime')"; $result =mysql_query($sql); if($result){ echo "<script>alert('添加文章成功');location.href='newlist.php'</script>"; }else{ echo "<script>alert('添加文章失敗');history.go(-1);</script>"; } } ?>
提交重置代碼