PHP開発ニュース管理システムの追加機能の実装

変更機能の実装については、次のフローチャートを見てみましょう

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">
                标题:<input type="text" name="title"></br></br>
                内容:<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 ファイルのコードを見てみましょう:

最初にデータベースに接続する必要があります。追加とは、フォームから情報を取得してデータベースに追加することです。データベースに接続する必要があります

コードは次のとおりです:

header("Content- type: text/html; charset=utf-8");//エンコーディングを設定します
$con =@mysql_connect(" localhost","root","root") または die("データベース接続に失敗しました");
mysql_select_db( 'ニュース') または die("指定されたデータベースを開けません");
mysql_query("set names utf8") ;//データベース

の文字セットを設定し、フォーム情報を取得します:

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

データベースに追加する前に、まずテキスト ボックスのタイトルと内容が次のとおりであるかどうかを判断する必要があります。空の場合はプロンプトが表示されます。コードは次のとおりです。

if (EMPTY ($ タイトル)) {
echo "& lt; スクリプト & gt; アラート ('タイトルを入力してください'); History.go (-1); </script>";
}elseif(empty($content) ){
echo "<script>alert('コンテンツを入力してください');history.go(-1);</script>";
}

コンテンツが空でない場合は、コンテンツを追加できますコードは次のとおりです。

$sql = "新しい (title,content,messtime) 値('$title','$content' , '$messtime')"; gt; "; } else {
スクリプト & gt; アラート ('記事の追加に失敗しました'); 以下のように:

りー

学び続ける
||
<?php //链接数据库 header("Content-type: text/html; charset=utf-8");//设置编码 $con =@mysql_connect("localhost","root","root") or die("数据库连接失败"); mysql_select_db('news') or die("指定的数据库不能打开"); mysql_query("set names utf8");//设置数据库的字符集 //添加操作 $title = $_POST['title']; $content = $_POST['content']; $messtime = time(); if(empty($title)){ echo "<script>alert('请输入标题');history.go(-1);</script>"; }elseif(empty($content)){ echo "<script>alert('请输入内容');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>"; } } ?>
  • おすすめコース
  • コースウェアのダウンロード
現時点ではコースウェアはダウンロードできません。現在スタッフが整理中です。今後もこのコースにもっと注目してください〜
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!