<form action= "php_mysql_add.php" method= "post" > 用户名: <input type= "text" name= "user" /> <br/> 标题: <input type= "text" name= "title" /> <br/> 内容: <textarea name= "content" id= "" cols= "30" rows= "10" ></textarea> <br/> <input type= "submit" name= "submit" value= "发布留言" /> <br/> </form> |
连接到数据库的代码:
1
|
if (@ $_POST [ 'submit' ]){<br><span style= "line-height: 1.5;" ><span style= "color: #999999;" > //设置表单变量</span><br></span> $user = $_POST['user']; <br> $title = $_POST['title'];<br> $content = $_POST['content'];<br><span style="color: #999999;"> //连接到数据库</span><br><em style="line-height: 1.5;"><em><em> $conn = @mysql_connect("localhost","root","root") or die("数据库连接失败,请检查你的网络,稍后再试试");<br></em></em></em> |
//选定数据库
mysql_select_db("test");
//执行SQL语句(查询)
mysql_query("set names 'utf8'");
1
2
3
4
5
6
|
<em id= "__mceDel" ><em id= "__mceDel" ><em id= "__mceDel" ><em id= "__mceDel" > $sql = "INSERT INTO `test`.`message` (`id`,`user`, `title`, `content`, `lastdate`) VALUES (NULL, '$user', '$title', '$content', now())" ; mysql_query( $sql );<br><span style= "color: #999999;" > //关闭数库</span> mysql_close( $conn ); } ?> |