PHP开发 新闻发布系统之发布PHP页面

创建 new_post.php  文件

接收 new.html 页面传过来的数据,将数据保存到数据库中

4.jpg

代码如下

<?php
header("content-type:text/html;charset=utf8");
$title=$_POST['title'];
$content=$_POST['content'];
$time=date("y-m-d H:i:s");
$conn=mysqli_connect("localhost","root","root","News");
mysqli_set_charset($conn,'utf8'); //设定字符集
if($conn){
    $sql= "insert into new(title,content,cre_time) values('$title','$content','$time')";
    $que=mysqli_query($conn,$sql);//执行sql语句
    if($que){
        echo "<script>alert('发布成功,返回新闻列表页');location.href='new_list.php';</script>";
    }else{
        echo "<script>alert('发布失败');location='" . $_SERVER['HTTP_REFERER'] . "'</script>";
        exit;
    }
}
else{
    die("数据库连接失败" .mysqli_connect_error());
}
?>

提示:上面代码中$time=是获取当前的时间

当我们发布新闻成功之后会返回新闻列表页new_list.php



Weiter lernen
||
<?php header("content-type:text/html;charset=utf8"); $title=$_POST['title']; $content=$_POST['content']; $time=date("y-m-d H:i:s"); $conn=mysqli_connect("localhost","root","root","News"); mysqli_set_charset($conn,'utf8'); //设定字符集 if($conn){ $sql= "insert into new(title,content,cre_time) values('$title','$content','$time')"; $que=mysqli_query($conn,$sql);//执行sql语句 if($que){ echo "<script>alert('发布成功,返回新闻列表页');location.href='new_list.php';</script>"; }else{ echo "<script>alert('删除失败');location='" . $_SERVER['HTTP_REFERER'] . "'</script>"; exit; } } else{ die("数据库连接失败" .mysqli_connect_error()); } ?>
einreichenCode zurücksetzen