HTML部分:
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
-
<html xmlns="">
-
<head>
-
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-
<title>留言板项目</title>
-
</head>
-
-
<body>
-
<form action="pub.php" method="post">
-
<p>留言标题:<input type="text" name="title" /></p>
-
<p>留言者:<input type="text" name="usrname" /></p>
-
<p>留言内容:<br />
-
<textarea name="content" cols="10" rows="10"></textarea>
-
</p>
-
<p>
-
<input type="submit" value="提交留言" />
-
</p>
-
-
</form>
-
</body>
-
</html>
将数据保存到数据库,将中的问号和p中间不能有空格;
-
<?php
-
-
//获取环境变量值
-
$title = $_POST['title'];
-
-
$usrname = $_POST['usrname'];
-
-
$content = $_POST['content'];
-
-
//链接数据库
-
-
$conn = mysql_connect('localhost','root','111111');
-
-
$sql = 'use php';
-
-
mysql_query($sql,$conn);
-
-
$sql = "insert into msg (title,name,content) values ('$title','$usrname','$content')";
-
-
$rs = mysql_query($sql,$conn);
-
-
if($rs){
-
echo '留言成功';
-
}
-
else{
-
echo '留言失败';
-
echo mysql_error();//该函数获取失败错误代码;
-
}
-
-
-
?>
查看数据库内容
-
<?php
-
-
$conn = mysql_connect('localhost','root','111111');
-
-
$sql = 'use php';
-
-
mysql_query($sql,$conn);
-
-
$sql = 'select * from msg';
-
-
$rs = mysql_query($sql,$conn);
-
-
//var_dump($rs);
-
-
//拿数据
-
-
//var_dump(mysql_fetch_assoc($rs));
-
-
$list = array();
-
-
while($row = mysql_fetch_assoc($rs)){
-
$list[] = $row; //将$row保存到数组$list数组中
-
}
-
-
print_r($list);
-
-
//查看数据库内容
-
/*
-
$id = $_GET['id'] + 0;//转换为整型;
-
-
if($id == 0){
-
echo '参数有误';
-
exit;
-
}
-
-
$conn = mysql_connect('localhost','root','111111');
-
-
$sql = 'use php';
-
-
mysql_query($sql,$conn);
-
-
$sql = "select * from msg where id = $id";
-
-
$rs = mysql_query($sql,$conn);
-
-
$msg = mysql_fetch_assoc($rs);
-
-
if(!$msg){
-
echo '参数有误';
-
exit;
-
}
-
-
print_r($msg);
-
-
-
*/
-
-
-
?>
阅读(2429) | 评论(0) | 转发(0) |