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

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']:"";     //標(biāo)題
$author = isset($_POST['author'])?$_POST['author']:"";    //作者
$content = isset($_POST['content'])?$_POST['content']:"";  //新聞內(nèi)容
$created_at = date("Y-m-d H:i:s");    //發(fā)布時間
$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);      //執(zhí)行sql語句
?>

Show the complete code publish.php file:

<?php
 header("content-type:text/html;charset=utf8");
 date_default_timezone_set('Asia/Shanghai');
  //連接數(shù)據(jù)庫
  $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");
 //執(zhí)行插入語句
 $sql="insert into new(title,author,content,created_at,updated_at) values('$title','$author','$content','$created_at','$updated_at')";
 $rel = mysqli_query($link,$sql);
 //執(zhí)行sql語句
 if($rel){
   echo "<script>alert('新聞發(fā)布成功');window.location.href='list.php'</script>";  //發(fā)布成功跳轉(zhuǎn)到新聞列表頁list.php
 }else{
   echo "<script>alert('新聞發(fā)布失敗');window.location.href='publish.php'</script>";
 }
?>


Continuing Learning
||
<?php header("content-type:text/html;charset=utf8"); date_default_timezone_set('Asia/Shanghai'); //連接數(shù)據(jù)庫 $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"); //執(zhí)行插入語句 $sql="insert into new(title,author,content,created_at,updated_at) values('$title','$author','$content','$created_at','$updated_at')"; $rel = mysqli_query($link,$sql); //執(zhí)行sql語句 if($rel){ echo "<script>alert('新聞發(fā)布成功');window.location.href='list.php'</script>"; //發(fā)布成功跳轉(zhuǎn)到新聞列表頁list.php }else{ echo "<script>alert('新聞發(fā)布失敗');window.location.href='publish.php'</script>"; } ?>
submitReset Code