PHP 表单提交都数据库失败
表单提交都数据库中;数据库是空的?我是菜鸟求教...<html><head></head><body><formaction="liuyan.php"method="po...
表单提交都数据库中;数据库是空的?我是菜鸟求教...
<html>
<head></head>
<body>
<form action="liuyan.php" method="post" name="name11">
<div align=center>留言板</div>
<table cellspacing=0 bordercolordark=#fff width=60% bordercolorlight=#000 border=1 align="center" cellpadding="2">
<tr>
<td width=100% height=300></td>
</tr>
<tr>
<td width=30% align="center" height=50>留言人:</td>
<td width=70% height=50><input type="text" name="user" size="20" maxlength="21"></td>
</tr>
<tr>
<td width=30% align="center" height=120>留言内容</td>
<td width=70% height=120><textarea name="post_contents" rows="10" cols="50"></textarea></td>
</tr>
<tr>
<td height="25" colspan="2" align="center">
<input type="reset" value="重置留言">
<input type="submit" name="submit" value="提交留言">
</td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------
<?php
$name=$_POST['user'];
$content=$_POST['post_contents'];
$conn=mysql_connect("localhost","admin","");
mysql_query("set name GB2312");
mysql_select_db("gbook");
$exec="INSERT INTO contents(name,content) VALUES(".$_POST['user'].",".$_POST['post_contents'].")";
echo $exec;
$result=mysql_query($exec);
if($exec>0)
echo "添加留言成功,谢谢你的留言!" ;
else
echo "添加留言失败";
echo "<p><a href=index1.php ><center>返回主页面</center></a>";
mysql_close();
?> 展开
<html>
<head></head>
<body>
<form action="liuyan.php" method="post" name="name11">
<div align=center>留言板</div>
<table cellspacing=0 bordercolordark=#fff width=60% bordercolorlight=#000 border=1 align="center" cellpadding="2">
<tr>
<td width=100% height=300></td>
</tr>
<tr>
<td width=30% align="center" height=50>留言人:</td>
<td width=70% height=50><input type="text" name="user" size="20" maxlength="21"></td>
</tr>
<tr>
<td width=30% align="center" height=120>留言内容</td>
<td width=70% height=120><textarea name="post_contents" rows="10" cols="50"></textarea></td>
</tr>
<tr>
<td height="25" colspan="2" align="center">
<input type="reset" value="重置留言">
<input type="submit" name="submit" value="提交留言">
</td>
</tr>
</table>
</form>
</body>
</html>
---------------------------------------------------------------------
<?php
$name=$_POST['user'];
$content=$_POST['post_contents'];
$conn=mysql_connect("localhost","admin","");
mysql_query("set name GB2312");
mysql_select_db("gbook");
$exec="INSERT INTO contents(name,content) VALUES(".$_POST['user'].",".$_POST['post_contents'].")";
echo $exec;
$result=mysql_query($exec);
if($exec>0)
echo "添加留言成功,谢谢你的留言!" ;
else
echo "添加留言失败";
echo "<p><a href=index1.php ><center>返回主页面</center></a>";
mysql_close();
?> 展开
2个回答
2015-10-21 · 知道合伙人软件行家
关注
展开全部
<?php
$name=$_POST['user'];
$content=$_POST['post_contents'];
$conn=mysql_connect("localhost","admin","");
//做数据库连接判断
if(!$conn){
die("could not connect to the database:</br>".mysql_error());//诊断连接错误
}
mysql_query("set names GB2312");//这里是names 不是 name
$db_selecct=mysql_select_db('gbook');//选择数据库
//选择库是不是成功了
if(!$db_selecct)
{
die("could not to the database</br>".mysql_error());
}
//sql 语句中是字符串类型的外边要加单引号
$exec="INSERT INTO contents(name,content) VALUES('".$_POST['user']."','".$_POST['post_contents']."')";
$result=mysql_query($exec);
//这里是判断 $result 不是判断$exec;
if($result)
echo "添加留言成功,谢谢你的留言!" ;
else
echo "添加留言失败";
echo "<p><a href=index1.php ><center>返回主页面</center></a>";
mysql_close();
?>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询