php写的一个简单提交数据代码没提示错误 但是数据库添加不进去
index.php代码如下:
<?php
require_once 'conn.php';
$id = $_POST[userid];
$name = $_POST[username];
$content = $_POST[content];
$sql = "insert into all(userid,name,content)values('$id','$name','$content')";
$res = mysql_query($sql);
if(!$res)
{
echo "数据添加失败!";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html ;charset=utf-8">
</head>
<body>
<form action="" method="post">
编号:<input type="text" name="userid"/><br/>
用户:<input type="text" name="username"/><br/>
留言:<textarea name="content"></textarea><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
conn.php代码如下
<?php
$conn = mysql_connect("localhost","root","root")or die("数据库连接失败!");
$db = mysql_select_db("web");
mysql_query("set names utf-8");
?>
小弟在此谢谢大家啦!
在insert语句前把要构造的sql语句echo出来,然后在mysql中直接运行一下,看有什么错误提示,在行修改。
all是关键字,values的前后加上空格
你应该判断下有没有数据吧。。要不第一次访问就是没有数据的,而且all是sql的关键字,用[]括起,获取提交的数据键名称是字符串。
require_once 'conn.php';
$id = $_POST["userid"];
$name = $_POST["username"];
$content = $_POST["content"];
if($id){
$sql = "insert into [all](userid,name,content)values('$id','$name','$content')";
$res = mysql_query($sql);
if(!$res)
{
echo "数据添加失败!";
}
}
修改为
<?php
require_once 'conn.php';
$id = $_POST["userid"];
$name = $_POST["username"];
$content = $_POST["content"];
$sql = "insert into all (userid,name,content) values ('$id','$name','$content')";
$res = mysql_query($sql);
if(!$res)
{
echo "数据添加失败!";
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html ;charset=utf-8">
</head>
<body>
<form action="" method="post">
编号:<input type="text" name="userid"/><br/>
用户:<input type="text" name="username"/><br/>
留言:<textarea name="content"></textarea><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
接收post要写引号,sql语句变量要单引号加双号包起