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> 展开
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> 展开
3个回答
展开全部
对于这个问题,从下面二方面着手:
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>
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>
追问
追答
@$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"];
展开全部
你试试这样行不行?
//接受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);
//接受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);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
value改为values试试,应该是这个问题的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询