??? ?? ?? ??? ?? ?? ??? PHP ??? PHP ??
? ????? ?? PHP ??? ???? ??????? ???? ???? ?? ?? ?? ???? ?????.
?? ????:
??? ???? ??? ?? ??? ?????. ???, ???, ?? ?? id, ??? ?? ??_at ? ?? ?? ????_at? ??? ???? ?????. ?? ????? ?? ???? ??? ???? ??? ??? ????. ??? ??? ? MySQL ?? ???? ??????? ?????.
????? ??? ??(created_at)? ?? ??(modified_at)? ?????. ??? ?? ?? ??? ??? ?? ???? ?? ?????.
date() ??? ???? ???. ?????? ? ?? ?? ??? ?? ???? ?????.
??? ??? ??? ?? ? ????
date("Y-m-d")? ?-?-?? ???? ?? ?????.
date("H:i:s")? ?-?-?? ???? ?? ?????
??? ?? ??? ???? ???? ??? ???? date_default_timezone_set('Asia/Shanghai')? ???? ???? ??? ???? ?????.
?? ?? ??????? ???? ???. ???? test?? ??????? ????.
<?php $link = mysqli_connect('localhost','username','password','test'); if (!$link) { die("連接失敗:".mysqli_connect_error()); } ?>
POST ???? ???? ???? ????
<?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ā)布時(shí)間 $updated_at = date("Y-m-d H:i:s"); //修改時(shí)間 ?>
insert into() ??: ?????? ???? ??? ??(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語(yǔ)句 ?>
?? ?? ??Publish.php ??:
<?php header("content-type:text/html;charset=utf8"); date_default_timezone_set('Asia/Shanghai'); //連接數(shù)據(jù)庫(kù) $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í)行插入語(yǔ)句 $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語(yǔ)句 if($rel){ echo "<script>alert('新聞發(fā)布成功');window.location.href='list.php'</script>"; //發(fā)布成功跳轉(zhuǎn)到新聞列表頁(yè)list.php }else{ echo "<script>alert('新聞發(fā)布失敗');window.location.href='publish.php'</script>"; } ?>