form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>這是標題</title>
<style>
body{
background-color: transparent;
## </style>
<script>
function foo() {
if(my .value
myform.title.focus();
return false;
}
if(myform.content.value==""){
);
myform.content.focus();
return false;
}
}
</script>
</head>##< ;body>
<form method="post" action="new_post.php" name="myform" onsubmit="return foo();">
<h1>這是新聞系統< /h1>
<p>標題:<input type="text" name="title"></p>
<p>內容:<textarea cols="30" rows="30" rows ="5" name="content"></textarea></p>
<p><input type="submit" value="發布新聞"></p>
</form>
</body>
</html>
new_post.php
############################################## ###<?php###
header("Content-type;text/html;charset=utf-8");
$_POST['title'] = empty($_POST['title']) ? '' : $_POST[ 'title'];
$_POST['content'] = empty($_POST['content']) ? '' : $_POST['content'];
$title=$_POST['title' ];
$content=$_POST['content'];
$time=date("Y-m-d H:i:s");
$conn = mysqli_connect('localhost',' root','root','new','30');
mysqli_set_charset($conn,'utf8');
if($conn){
$sql="insert into news(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{## ("資料庫連線失敗".mysqli_connect_error());
}
}
資料插不進資料庫,有很多種情況哦,有沒有報錯資訊呢,如果沒有的話,就斷點調試找問題!
印下$sql語句觀察。