当前位置:编程学习 > php >>

php留言板

/************/
config.php
/***********/
<?php
    $host = 'localhost';            // 数据库服务器
    $user = 'root';            // 数据库用户名
    $password = '123456';            // 数据库密码
    $db = 'project';            // 数据库名

//打开数据库
$conn = mysql_pconnect ($host,$user,$password);
if(!$db)
{
die('<b>数据库连接失败!</b>');
}
//选择数据库
mysql_select_db ($db);
?>
/*********************/
index.php
/********************/

<html>
<body>
<script type="text/javascript">
function check_form(obj){
    if(document.getElementById('post_name').value==''){
        alert('写一下您的大名吧');
        document.getElementById('post_name').focus();
        return false;
    }
    if(document.getElementById('post_body').value==''){
        alert('不是来捣乱的吧,写一下留言内容吧!');
        document.getElementById('post_body').focus();
        return false;
    }
    return true;

}
</script>
        <form action="/sent.php" method="post" onSubmit="return check_form(this)">
      <table width="100%"  border="0">
        <tr>
          <td align="right">您的大名</td>
          <td><input name="post_name" id="post_name" type="text" size="20" maxlength="20"></td>
        </tr>
                <td align="right">您的发言</td>
<td><textarea name="post_body" id="post_body" rows="10" cols="50">您的发言</textarea></td>
        <tr>
          <td align="right"> </td>
          <td><input type="submit" name="Submit" value="提        交" ></td>
        </tr>
      </table>
    </form></td>
        </body>
        <html>
<?php
    include('get.php')
?>
/***************************/
get.php
/****************************/
[quote]
<?php
include('config.php');
$query = "SELECT * FROM project_post";
$result = mysql_query($query);

while($row = mysql_fetch_array($result)) echo "姓名:$row[post_name] </br> 内容: $row[post_body]</br>";
?>
/***************************/
sent.php
/***************************/

<?php
include('config.php');
$post_name=addslashes($_POST['post_name']);
$post_body=addslashes($_POST['post_body']);
$sql = "INSERT INTO project_post (post_name,post_body) VALUES('$post_name','$post_body')";
mysql_query($sql);
header('location:/index.php');
?>

 

补充:Php教程,Php常用代码 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,