PHP development...LOGIN

PHP development of simple news release system news release page PHP code

In this section, we use the background PHP code to add data to the database and display it on the news list page

Main idea:

Fill in the news in the form The content includes: title, author, content, other field id, release time created_at, modification time updated_at. The content is completed by the server. Of course, you have to write the program yourself, but you don’t have to do it manually. After submitting the form, use MySQL statements to add them to the database.

The release time created_at and modification time updated_at will be used in the example. We will directly set them to the current release time and modification time.

You need to use the date() function: format the timestamp into a more readable date and time.

You can get simple dates and times

date("Y-m-d") means getting the year-month-day

date("H:i:s" ) means getting hours-minutes-seconds

Because we all use the Eastern Hemisphere time zone, here we use date_default_timezone_set('Asia/Shanghai') to set the time zone to the time zone of Shanghai.

1605.png

Of course we still need to connect to the database first. Here we create a database named test.

<?php
$link = mysqli_connect('localhost','username','password','test');
if (!$link) {
  die("连接失败:".mysqli_connect_error());
}
?>

We use the POST method to obtain data

<?php
$title = isset($_POST['title'])?$_POST['title']:"";     //标题
$author = isset($_POST['author'])?$_POST['author']:"";    //作者
$content = isset($_POST['content'])?$_POST['content']:"";  //新闻内容
$created_at = date("Y-m-d H:i:s");    //发布时间
$updated_at = date("Y-m-d H:i:s");    //修改时间
?>

Use insert into(): add data to the database table (create a table named new),

<?php
$sql="insert into new(title,author,content,created_at,updated_at) values('$title','$author','$content','$created_at','$updated_at')";

$rel = mysqli_query($link,$sql);      //执行sql语句
?>

Show the complete code publish.php file:

<?php
 header("content-type:text/html;charset=utf8");
 date_default_timezone_set('Asia/Shanghai');
  //连接数据库
  $link = mysqli_connect('localhost','username','password','test');
  if (!$link) {
    die("连接失败:".mysqli_connect_error());
  }
 $title = isset($_POST['title'])?$_POST['title']:"";
  $author = isset($_POST['author'])?$_POST['author']:"";
  $content = isset($_POST['content'])?$_POST['content']:"";
 $created_at = date("Y-m-d H:i:s");
 $updated_at = date("Y-m-d H:i:s");
 //执行插入语句
 $sql="insert into new(title,author,content,created_at,updated_at) values('$title','$author','$content','$created_at','$updated_at')";
 $rel = mysqli_query($link,$sql);
 //执行sql语句
 if($rel){
   echo "<script>alert('新闻发布成功');window.location.href='list.php'</script>";  //发布成功跳转到新闻列表页list.php
 }else{
   echo "<script>alert('新闻发布失败');window.location.href='publish.php'</script>";
 }
?>


Next Section
<?php header("content-type:text/html;charset=utf8"); date_default_timezone_set('Asia/Shanghai'); //连接数据库 $link = mysqli_connect('localhost','username','password','test'); if (!$link) { die("连接失败:".mysqli_connect_error()); } $title = isset($_POST['title'])?$_POST['title']:""; $author = isset($_POST['author'])?$_POST['author']:""; $content = isset($_POST['content'])?$_POST['content']:""; $created_at = date("Y-m-d H:i:s"); $updated_at = date("Y-m-d H:i:s"); //执行插入语句 $sql="insert into new(title,author,content,created_at,updated_at) values('$title','$author','$content','$created_at','$updated_at')"; $rel = mysqli_query($link,$sql); //执行sql语句 if($rel){ echo "<script>alert('新闻发布成功');window.location.href='list.php'</script>"; //发布成功跳转到新闻列表页list.php }else{ echo "<script>alert('新闻发布失败');window.location.href='publish.php'</script>"; } ?>
submitReset Code
ChapterCourseware