文章来源:http://www.phphubei.com/thread-297-1-1.html关于表单提交无法写入数据库
//form.php<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<form action="insert.php" method="post" name="form1" id="form1">
<table width="300" border="0" align="center">
<tr>
<td>学号</td>
<td><label>
<input type="text" name="number" />
</label></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>QQ</td>
<td><input type="text" name="qq" /></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label></td>
</tr>
</table>
</form>
</body>
</html>
//insert.php<? $mysql_server_name = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "cna";
$sql = "INSERT INTO `communication` ( `id` , `number` , `name` , `qq` , `email` , `time` )
VALUES (
'null', '$number', '$name', '$qq', '$email', NOW( )
);";
$conn=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
$id = mysql_insert_id();
mysql_close($conn);
header("Location:form.php");
?>
在数据库中显示的是空白数据记录.请问这是怎么回事呢!谢谢!
//form.php<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head><body>
<form action="insert.php" method="post" name="form1" id="form1">
<table width="300" border="0" align="center">
<tr>
<td>学号</td>
<td><label>
<input type="text" name="number" />
</label></td>
</tr>
<tr>
<td>姓名</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>QQ</td>
<td><input type="text" name="qq" /></td>
</tr>
<tr>
<td>email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td colspan="2"><label>
<div align="center">
<input type="submit" name="Submit" value="提交" />
</div>
</label></td>
</tr>
</table>
</form>
</body>
</html>
//insert.php<? $mysql_server_name = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "cna";
$sql = "INSERT INTO `communication` ( `id` , `number` , `name` , `qq` , `email` , `time` )
VALUES (
'null', '$number', '$name', '$qq', '$email', NOW( )
);";
$conn=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
$id = mysql_insert_id();
mysql_close($conn);
header("Location:form.php");
?>
在数据库中显示的是空白数据记录.请问这是怎么回事呢!谢谢!
VALUES (
'$number', '$name', '$qq', '$email', NOW( )
);";
id设为主键,auto_increment
$mysql_server_name = "localhost";
$mysql_username = "root";
$mysql_password = "";
$mysql_database = "cna";// 表单提交的数据在$_POST中
$number = $_POST['number'];
$name = $_POST['name'];
$qq = $_POST['qq'];
$email = $_POST['email'];$sql = "INSERT INTO communication (id , number , name , qq , email , time)";
$sql .= "VALUES ('null', '$number', '$name', '$qq', '$email', NOW());";$conn=mysql_connect( $mysql_server_name, $mysql_username, $mysql_password);
mysql_select_db($mysql_database,$conn);
$result = mysql_query($sql);
$id = mysql_insert_id();
mysql_close($conn);header("Location:form.php");
?>