php提交表单时用$_POST[]不能成功传递变量?

想做一个简单的提交表单后写入数据库的php,可是老是在提交表单时出问题,求大神指教,代码如下:add.html<html><head><title>Index</titl... 想做一个简单的提交表单后写入数据库的php,可是老是在提交表单时出问题,求大神指教,代码如下:
add.html
<html>
<head>
<title>Index</title>
</head>
<body>
<center>
<h1>Add data</h1>
<form method = "post" action = "add.php">
name:<input type = "text" name = "name"/><br><br>
age:<input type = "text" name = "age"/><br><br>
phone:<input type = "text" name = "phone"/><br><br>
address:<input type = "text" name = "address"/><br><br>
education:<input type = "text" name = "education"/><br><br>
<input type = "submit" value = "submit">
<input type = "reset" value = "reset">
</form>
</center>
</body>
</html>

add.php
<html>
<head>
<title>add result</title>
</head>
<body>
<center><h1>Add result</h1></center>
<?php
$username = "root";
$password = "19941230";
$database = "mydb";
$connect = mysql_connect("localhost",$username,$password);
if($connect)
{
@$name = $_POST["$name"];
@$age = $_POST["$age"];
@$phone = $_POST["$phone"];
@$address = $_POST["$address"];
@$education = $_POST["$education"];
echo $name;
@$insertquery = "INSERT INTO 'user2'('name','age','phone','address','education')
value('$name','$age','$phone','$address','$education')";
mysql_select_db($database);
mysql_query($insertquery);
echo"data add success!";
mysql_close($connect);
}
else
{
echo"connect failed";
}
?>
</body>
<html>
展开
 我来答
pyllion
2012-08-03 · TA获得超过817个赞
知道小有建树答主
回答量:661
采纳率:100%
帮助的人:659万
展开全部
对于这个问题,从下面二方面着手:
1.把insert 语句中的
@$insertquery = "INSERT INTO 'user2'('name','age','phone','address','education')
value('$name','$age','$phone','$address','$education')";
改为:
@$insertquery = "INSERT INTO user2(name,age,phone,address,education)
values('$name','$age','$phone','$address','$education')";

2.若还是加不到数据库中,就证明这些字段类型是有些不符合建表规则的,如age字段应该是数字那么单引号得去掉等等;

3.下面代码是把所有表字段设置为字符型,经我测试过的:

add.html
<html>
<head>
<title>Index</title>
</head>
<body>
<center>
<h1>Add data</h1>
<form method = "post" action = "add.php">
name:<input type = "text" name = "name"/><br><br>
age:<input type = "text" name = "age"/><br><br>
phone:<input type = "text" name = "phone"/><br><br>
address:<input type = "text" name = "address"/><br><br>
education:<input type = "text" name = "education"/><br><br>
<input type = "submit" value = "submit">
<input type = "reset" value = "reset">
</form>
</center>
</body>
</html>

add.php
<html>
<head>
<title>add result</title>
</head>
<body>
<center><h1>Add result</h1></center>
<?php
$username = "root";
$password = "19941230";
$database = "mydb";
$connect = mysql_connect("localhost",$username,$password);
if($connect)
{
@$name = $_POST["$name"];
@$age = $_POST["$age"];
@$phone = $_POST["$phone"];
@$address = $_POST["$address"];
@$education = $_POST["$education"];
echo $name;
@$insertquery = "INSERT INTO user2(name,age,phone,address,education)
values('$name','$age','$phone','$address','$education')";
mysql_select_db($database);
mysql_query($insertquery);
echo"data add success!";
mysql_close($connect);
}
else
{
echo"connect failed";
}
?>
</body>
<html>
追问

感谢您在百忙之中帮我解答,按照您给的代码已经试了,确实可以插入一条新的数据,可是各个字段的值并不是表单输入的值

我估计这个问题还是表单根本没有把数据传过来吧,因为在add.php中我用$_POST传完值后,加了一句echo"$name"让它输出一个值,可是运行时却没出现

所以我感觉数据库插入是没问题的,可能还是表单提交时有问题,我装的是wampserver,没有配置过,可能和它有关吗?

追答
@$name = $_POST["$name"];
@$age = $_POST["$age"];
@$phone = $_POST["$phone"];
@$address = $_POST["$address"];
@$education = $_POST["$education"];

改成:
$name = $_POST["name"];
$age = $_POST["age"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$education = $_POST["education"];
91好帅
2012-08-03
知道答主
回答量:15
采纳率:0%
帮助的人:5.1万
展开全部
你试试这样行不行?
//接受add.html传送的数据
$name = $_POST["name"];
$age = $_POST["age"];
$phone = $_POST["phone"];
$address = $_POST["address"];
$education = $_POST["education"];

//连接数据库
$connect = mysql_connect("localhost","root","19941230");
//选择数据库
mysql_select_db("mydb",$connect);
//往表user2插入数据
$insertquery = "INSERT INTO 'user2'('name','age','phone','address','education')
values('$name','$age','$phone','$address','$education')";
$result=mysql_query($insertquery );
//判断是否插入成功
$row=mysql_fetch_array($result);
if($row){
echo "成功!";
}else{
echo "失败";
}

mysql_close($connect);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
流氓2vs流氓3
2012-08-03
知道答主
回答量:33
采纳率:0%
帮助的人:24.8万
展开全部
value改为values试试,应该是这个问题的
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式