HTML页面
<html>
<head>
<title>向数据库中插入数据</title>
</head>
<link href ="style.css" rel="stylesheet" type="text/css"/>
<body>
<form action="insert.php" method="POST">
<h2>访客留言</h2>
<fieldset>
<legend>发表评论</legend>
姓 名:
<input name="stuname" type="text" maxlength="32" id="stuname" size="30" />
<br/>
主 页:
<input name="website" type="text" maxlength="256" id="website" size="30" />
<br/>
<textarea name="appreciation" rows="5" cols="40" id="appreciation" ></textarea>
<br/>
<input type="submit" name="submit1" id="sub" value="提交" />
<input type="reset" name="reset1" id="res" value="重置"/>
</fieldset>
</form>
</body>
</html>
PHP页面
<?php
$con = mysql_connect("localhost","root","sa");
if(!$con)
{echo "数据库连接失败".mysql_error(); }
mysql_select_db("mytable"); //选择数据库
$name = $_POST['stuname'];
$website = $_POST['website'];
$note = $_POST['appreciation'];
$sql = "insert into guestnote values('$name','$website','$note', now())";
$r =mysql_query($sql);
if($r)
{
echo "成功回复信息";
}
else
{
echo "回复信息失败".mysql_error();
}
mysql_close();
?>
弄了一个下午,本来今天是第二天接触MySQL,呵呵。MySQL的好的东西不会,本来在数据库中添加了一个ID,自增,但是当提交的时候一直无法获取ID的新值,一气之下,把ID删了。
呵呵。如果有人能将自增的ID值也插入到数据库中的话,please talk me.
ID初始值为1。
向数据库插入数据
本文介绍了一个简单的HTML表单,用于收集用户输入的数据,并通过PHP脚本将其插入到MySQL数据库中。涉及HTML表单元素及PHP操作数据库的基本语法。

被折叠的 条评论
为什么被折叠?



