php 增加数据保存到mysql数据库实例教程
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>php教程 增加数据保存到mysql教程数据实例教程</title> <style type="text/css教程"> <!-- body { margin-left: 0px; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; } td{ padding:2px; } body,td,th { font-size: 12px; } --> </style></head> <body> <table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#F2F2F2" > <form id="form1" name="form1" method="post" action=""><tr> <td width="15%" height="26" align="center" bgcolor="#FFFFFF">标 题:</td> <td width="85%" bgcolor="#FFFFFF"> <label> <input type="text" name="title" /> </label> </td> </tr> <tr> <td height="26" align="center" bgcolor="#FFFFFF">关键词:</td> <td bgcolor="#FFFFFF"><label> <input name="keyword" type="text" id="keyword" size="50" /> </label></td> </tr> <tr> <td height="26" align="center" bgcolor="#FFFFFF">描 述:</td> <td bgcolor="#FFFFFF"><label> <textarea name="desc" cols="50" rows="4" id="desc"></textarea> </label></td> </tr> <tr> <td height="26" align="center" bgcolor="#FFFFFF">说明简介:</td> <td bgcolor="#FFFFFF"><label> <textarea name="body" cols="80" rows="8"></textarea> </label></td> </tr> <tr> <td height="26" align="center" bgcolor="#FFFFFF"> </td> <td bgcolor="#FFFFFF"><label> <input type="submit" name="Submit" value="保存数据" /> </label></td> </tr> </form> </table> <?php if ($_POST) { mysql_connect('localhost', 'root', 'root'); mysql_select_db('fangke_cc'); mysql_query("set names 'gbk'"); $title = htmlspecialchars(isset($_POST['title']) ? $_POST['title'] : ''); $keyword = htmlspecialchars(isset($_POST['keyword']) ? $_POST['keyword'] : ''); $desc = htmlspecialchars(isset($_POST['desc']) ? $_POST['desc'] : ''); $body = htmlspecialchars(isset($_POST['body']) ? $_POST['body'] : ''); if ($title == "" || strlen($title) > 200) { exit('<script>alert("标题不合法,重新输入信息");history.back();</script>'); } else if ($body == "" || strlen($body) > 2048) { exit('<script>alert("内空太长,重新输入信息");history.back();</script>'); } else { $time = time(); $body = preg_replace("/$title/", "<h1>" . $title . "</h1>", $body, 1); $sql = "insert into su_zt(title,description,keyword,body,datetimes) values('$title','$desc','$keyword','$body','$time')"; mysql_query($sql) or die(mysql_error() . $sql); die('<script>alert("保存成功");location="' . $_SERVER['HTTP_REFERER'] . '";</script>'); } } ?>
su_zt表结构
CREATE TABLE IF NOT EXISTS `su_zt` ( `id` int(8) NOT NULL auto_increment, `title` varchar(200) character set gb2312 default NULL, `keyword` varchar(60) character set gb2312 default NULL, `description` text character set gb2312, `body` text character set gb2312, `datetimes` int(4) default NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;
</body>
</html>
教程地址:http://www.phprm.com/code/545bd1aaa16eb9bfa3af871304a1ff37.html
欢迎转载!但请带上文章地址^^