代码如下:
<?php
if(isset($_POST['submit'])) {
$title = $_POST['title'];
$authorId = $_POST['authorId'];
$content = $_POST['content'];
//连接数据库
require_once "msql_connect.php";
//插入操作
$sql = "insert into 'msgs'('title', 'authorId', 'content') values(
'{$title}','{$authorId}','{$content}')";
$result = mysqli_query($link,$sql);
if($result){//插入成功
if(mysqli_affected_rows($link)==1){//判断是否插入一条记录
echo<<<STR
<script type="text/javascript">
alert('insert operation succed!');
window.location.href='index.php';
</script>
STR;
}
}else{
echo mysqli_error($link);
}
mysqli_close($link);
}
?><!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>添加留言</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
留言标题:<input type="text" name="title"/><br/>
<input type="hidden" name="authorId" value="1"/>
留言内容:<textarea name="content"></textarea><br/>
<input type="submit" value="发表"/>
</form>
</body>
</html>按理来说按下提交按钮后,会显示插入成功,并跳转回主页,但提交后,还是停留在添加页面中,变为如下: