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

用php插入数据只可以插入数字和字母插入不了文字
我先用$_POST获取了表单的两个数据然后提交给执行sql语句的页面但是总是一直插入不了文字
+++++++++++++++++++++++++++++++++++++++++++++++++++++
 <?php
 require "777.php";
 mysql_query("set names 'utf-8");
 mysql_query("SET NAMES 'GB2312'");
  $sql= mysql_query("select*from lyb"); //输出留言内容

  while( $sq = mysql_fetch_array($sql)){

  echo $sq[id];
  }

?>
=++++++++++++++++++++++++++++++++++++++++++++++++++++++


<?php
/*
 * Created on 2012-4-3
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 require "777.php";
 if($_POST[submit]!=""){

$user =$_POST[user];
$post = $_POST["intro"];

 $sql = "insert into lyb(user,post) values ('$user','$post')"; //执行sql插入语句页面

$qu= mysql_query($sql);
if($qu){
echo '<a href="http://localhost/cookie/mysql.php">查看留言</a>' ;


}


  }
?>
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

<?php
$db ="localhost";
$user="root";
$pwd = "123456";
$conn = mysql_connect("$db","$user","$pwd");
mysql_select_db("sjk",$conn);




?>


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++











------解决方案--------------------
你的第一段代码有输出吗?都能看到什么?
 mysql_query("set names 'utf-8");
应为
 mysql_query("set names 'utf8");

如果你的程序文件是 utf-8 编码的,用
 mysql_query("set names 'utf8");
如果你的程序文件是 gbk 编码的,用
 mysql_query("SET NAMES 'GB2312'");
最好是
 mysql_query("SET NAMES 'GBK'");

两者选一

第二段代码没有语言声明,即便插入了,也可能是乱码
$sql = "insert into lyb(user,post) values ('$user','$post')";
$qu= mysql_query($sql) or die(mysql_error());
看一下都报什么错