PHP 메시지 보드 개발 튜토리얼 - 메시지 추가하기
다음 HTML 코드를 살펴보겠습니다.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>留言板</title>
<style type="text/css">
*{margin:0px;padding:0px;}
body{background:#eee;}
#bdy{width:414px;height:736px;margin:0 auto;margin-top:20px;
background:#66CDAA;
}
#top{font-family:"隶书";font-size:30px;text-align:center;/*margin-top:18px;*/
color:#f60;}
.a{text-decoration:none;color:#fff;float:right;padding-right:15px;}
.a:hover{color:red;}
#cont{width:414px;height:736px;margin:0 auto;margin-top:20px;}
#left{width:350px;height:300px;margin-top:80px;margin-left:15px;/*float:left;*/
background:#48D1CC;padding-left:5px;}
#right{width:360px;height:200px;margin-top:20px;background:#48D1CC;
margin-left:15px;/*float:left;*/}
h5{text-align:center;margin-top:15px;margin-bottom:20px;}
#sub{width:120px;height:25px;margin-top:15px;}
#sub:hover{background:#AFEEEE;}
.span{font-size:18px;color:red;font-weight:bold;}
table{width:360px;margin:0 auto;margin-top:15px;border:1px solid #eee;}
td{text-align:center;}
#td a{text-decoration:none;color:#eee;}
#td a:hover{color:red;}
</style>
</head>
<body>
<div id="bdy">
<div id="top">留言板</div>
<a href="login.php" class="a">登录</a>
<a href="reg.php" class="a">注册</a>
<div id="cont">
<div id="left">
<h5>写留言</h5>
<form method="post" action="addmessage.php">
标题:<input type="text" placeholder="请输入标题" name="title">
</br></br>
内容:<textarea cols="40" rows="5" name="content"></textarea>
</br></br>
<input type="submit" value="添加留言" id="sub">
</form>
</div>
<div id="right"></div>
</div>
</div>
</body>
</html>양식은 addmessage.php에 제출됩니다. 다음 addmessage.php 파일을 자세히 살펴보겠습니다.
먼저 세션도 열어야 합니다.
session_start();
주의사항: 지금 오픈 seesion 나중에 판단할 수 있도록 로그인한 상태에서는 메시지를 남겨도 되고, 그렇지 않으면 메시지를 남길 수 없습니다
연결 데이터베이스 파일 conn.php를 소개합니다
require_once(' conn.php');
문자 인코딩 설정
header("Content -type: text/html; charset=utf-8");//인코딩 설정
이제 양식 정보를 가져와야 합니다
$ title = $_POST['title'];
$content = $_POST['content '];
$messtime = time();
다음으로 메시지를 추가해야 합니다
제목과 내용이 채워지지 않은 경우 에, 우리는 그 사람이 제출하도록 해서는 안 됩니다
if(empty($title)){
echo "<script>alert('제목을 입력하세요'); History.go(-1);</script>" ; ('콘텐츠를 입력하세요');history.go(-1);</script>";
}
그렇지 않으면 다음 코드를 추가할 수 있습니다:
if(!empty($_SESSION[ 'name'])){
$sql = "메시(title,content,messtime) 값에 삽입('$title','$content','$messtime')"; 이 ~ ; 경고('메시지 추가 실패'); History.go (-1) & lt;/script & gt; "
echo" & lt; 로그인 후); 기록. go(-1);</script>";
}
세션['name']을 판단합니다. 비어 있지 않으면 로그인되었음을 의미합니다. 메시지를 추가할 수 있습니다. 그렇지 않으면 사용자에게 로그인 후 메시지를 남겨달라는 메시지가 표시됩니다
전체 코드는 다음과 같습니다:
<?php
session_start();
header("Content-type: text/html; charset=utf-8");//设置编码
require_once('conn.php');
$title = $_POST['title'];
$content = $_POST['content'];
$messtime = time();
if(empty($title)){
echo "<script>alert('请输入标题');history.go(-1);</script>";
}elseif(empty($content)){
echo "<script>alert('请输入内容');history.go(-1);</script>";
}else{
if(!empty($_SESSION['name'])){
$sql = "insert into mess (title,content,messtime) values('$title','$content','$messtime')";
$result =mysql_query($sql);
if($result){
echo "<script>alert('添加留言成功');location.href='message.php';</script>";
}else{
echo "<script>alert('添加留言失败');history.go(-1);</script>";
}
}else{
echo "<script>alert('请登录后添加留言');history.go(-1);</script>";
}
}
?>- 코스 추천
- 코스웨어 다운로드
현재 코스웨어를 다운로드할 수 없습니다. 현재 직원들이 정리하고 있습니다. 앞으로도 본 강좌에 많은 관심 부탁드립니다~ 















