日期:2014-05-17  浏览次数:20356 次

添加不进数据库,没报错,就是添加不进
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>添加信息</title>
</head>

<body>
<script language="javascript">
function check(form){
if(form.txt_biti.value=="")
{
alert("标题不能为空");form.txt_biti.focus();return false;
}
if(form.textfield.value=="")
{
alert("内容不能为空!!");form.textfield.focus();return false;

}
form.submit();

}
</script>
<form action="add_insert.php" method="post" name="form1">

<table width="450" border="0" cellspacing="0" cellpadding="0" align="center">
  
  <tr>
  <td width="150" height="70"><div align="center"> 标题:</div></td>
  <td><input name="txt_biti" type="text" size="40"/></td>
  </tr>
  <tr>
  <td width="100" height="70" ><div align="center">内容:</div></td>
  <td>
  <textarea name="textfield" cols="50" rows="8" id="textfield" height="100"></textarea>
  </div></td>
  </tr>
  <tr>
  <td colspan="2" height="50" align="center"><div align="center">
  <input name="Submit" type="submit" class="btn_grey" value="保存" onClick="return check(form1);"></td>
  </tr>
   
</table>
 </form>

</body>
</html>
下面是第二个页面;
<?php
  $conn=mysql_connect("localhost","root","root") or die("数据库服务器连接错误".mysql_error());
  mysql_select_db("title",$conn) or die("数据库访问错误".mysql_error());
  mysql_query("set names gb2312");
$title=$_POST[txt_title];
$content=$_POST[txt_content];

$sql=mysql_query("insert into tb_title(title,content)values('$title',$content)");
echo "<script>alert('公告信息添加成功!');window.location.href='insert.php';</script>";
mysql_free_result($sql);
mysql_close($conn);
?>
,数据库是title,表tb_title

------解决方案--------------------
把这行代码修改下$content需要加上单引号:
PHP code
$sql=mysql_query("insert into tb_title(title,content)values('$title','$content')");

------解决方案--------------------
把最后面的改成,同时提醒你插入是不需要销毁资源的,因为这根本就不是个资源.
PHP code
$sql=mysql_query("insert into tb_title(title,content)values('$title','$content')");
if(!sql){
   echo "<script>alert('公告信息添加失败!');history.back();</script>";
}else{
 echo ""<script>alert('公告信息添加成功!');window.location.href='insert.php';</script>";
}
mysql_close($conn);

------解决方案--------------------
$title=$_POST[txt_title];
$content=$_POST[txt_content];
这两句能加引号么,单引双引,咱引一下行么?
------解决方案--------------------
$sql=mysql_query("insert into tb_title(title,content)values('$title',$content)");
你最好把它拆分为:
$sql="insert into tb_title(title,content)values('$title',$content)";
mysql_query($sql);
的形式,而且你的$sql好像也不对,这样输入时不对的