php新闻发布系统post请求接收代码02

本文介绍了一个使用PHP实现的简单新闻发布系统的代码示例。该系统能够接收从new.html页面提交的数据,并将其保存到本地数据库中。文章详细展示了如何创建数据库、表结构以及执行数据插入操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下;

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/18
 * Time: 18:16
 * 这个问文件用来接收从new.html传递过来的文件
 * 将数据保存到数据库中
 */
//ini_set('date.timezone','Asia/Shanghai');
date_default_timezone_set('PRC');
header("content-type:text/html;charset=utf8");

echo "new_post页面<br/>";
$servername = "localhost";
$username = "root";
$password = "root";
$title = $_POST['title'];
$content = $_POST['content'];
$time = date("y-m-d H:i:s");
echo $title . "==" . $content . "tite=" . $time;
//设置数据库连接
$conn = mysqli_connect($servername, $username, $password);
mysqli_set_charset($conn, 'utf8');

if ($conn) {
    //数据库连接成功
    //创建数据库
    $sql = "create database if not exists mynews";
    if (mysqli_query($conn,$sql)) {
        //创建数据库成功
        //创建表
        $createTableSql="create table if NOT EXISTS mynews.mynews(_id INTEGER PRIMARY KEY  auto_increment,title VARCHAR(30) NOT NULL ,content text  NOT NULL ,cre_time datetime NOT NULL )";
        echo $createTableSql;
        echo '<br/>';
        $create=mysqli_query($conn,$createTableSql);
        if ($create){
            //插入语句
            $insertSql = "insert into mynews.mynews(title,content,cre_time) VALUES ('" . $title . "','".$content."','".$time."')";
            //执行插入操作
            echo $insertSql;

            $que = mysqli_query($conn, $insertSql);
            if ($que) {
                //echo "<script>alert('发布成功,返回新闻列表页');location.href='new_list.php';</script>";
                echo "<script>alert('发布成功,返回新闻列表页');location.href='new_list.php';</script>";

            } else {
                //插入失败
                "<script>alert('删除失败');location='" . $_SERVER['HTTP_REFERER'] . "'</script>";
                exit;
            }
        }else{
            echo "数据库表创建失败";
        }


    } else {

        //创建数据库失败
        echo "database error";
    }

} else {

    //数据库连接失败
    echo "数据库连接失败";
}
//新闻发布成功用了个die终止 显示效果如下:


PHP实例之新闻发布系统 Create TABLE `news` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY , `p_time` DATETIME NOT NULL , `title` VARCHAR( 80 ) NOT NULL , `detail` TEXT NOT NULL ) TYPE = innodb; create table news(id int not null auto_increment primary key, p_time datetime not null, title varchar(80)not null, detail text not null )type=innodb; 数据库连接:conn.php <? $conn = @mysql_connect('localhost','root','')or die(mysql_error()."不能连接到数据库!"); //连接数据库; $db = mysql_select_db('news',$conn); $page_size = 8; //每页最多显示新闻条数; ?> 添加新闻页面:new.php <? $title="新闻发布系统"; include("inc/header.inc");//头文件 ?> <style type="text/css"> <!-- .STYLE1 {font-size: 12px} .title { font-size: 12px; border-top-style: none; border-right-style: none; border-bottom-style: none; border-left-style: none; font-family: "宋体"; color: #993300; line-height: normal; height: 16px; } .field { font-family: "宋体"; font-size: 12px; color: #993333; } .STYLE2 { font-size: 16px; font-weight: bold; } --> </style> PHP+Mysql新闻发布 <form action="post.php" method="post" name="frm" id="frm"> 标题: <input name="title" type="text" class="title" id="title" size="60" maxlength="80"></td> 内容: <textarea name="textfield" cols="58" rows="6" class="field"></textarea> <input name="submit" type="submit" value="发布"></td> </form> <? include("inc/navbar.inc");//底部 ?> 新闻处理页面:post.php <? include"conn.php"; $title=htmlspecialchars($_POST['title']); $textfield=htmlspecialchars($_POST['textfield']); $pub_time=date('Y')."-".date('m')."-".date('d')." ".date('H').":".date('i').":".date('s'); $query="insert into news(title,detail,p_time)values ('$title','$textfield','$pub_time')"; $result=mysql_query($query); if($result) { echo "发布成功!"; echo "新闻列表 继续发布"; } else { echo mysql_error().""; echo "发布失败!请返回"; } ?> 新闻列表:list.php <? include "conn.php"; $query = "Select COUNT(*) FROM news"; $result = mysql_query($query); $num = mysql_num_rows($result); $page_count = ceil($num/$page_size); //$offset = ($page_count-1)*$page_size; if(empty($_GET['page'])) { $page = 1; }else { $page = $_GET['page']; if($page= $page_count; $page = $page_count-1; } } $query ="Select * FROM `news` orDER BY `id` DESC LIMIT ".($page-1)*$page_count.","."$page_size"; $result = mysql_query($query); ?> 标题 时间 <?php while($l_result = mysql_fetch_array($result)) { ?> <?php echo $l_result['id'];?> <?php echo $l_result['title'];?> <?php echo $l_result['p_time'];?> <?php } ?> 发布信息 <?php //页码显示 for ($i=1;$i<=($page_count-1);$i++){ echo "".$i." "; } //页码显示 ?> 新闻显示页面:view.php <? include "conn.php"; $query = "Select * FROM news where id=".$_GET['id']; $result = mysql_query($query); $v_result = @mysql_fetch_array($result); ?> <table width="60%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#0066FF"> 标题: <?php echo $v_result['title'];?> 内容: <?php echo $v_result['detail'];?> 返回
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值