我已经在<?php那里或者sql语句之前设置utf8,可是插入的数据还是乱码,求大神解答啊
代码如下
<?php
error_reporting(0);
header("Content-Type:text/html;charset=utf-8");
if(isset($_POST["Submit"]) && $_POST["Submit"] == "提 交")
{
mysql_connect("localhost","root",""); //连接数据库
mysql_select_db("xt"); //选择数据库
mysql_query("set names 'gdk'"); //设定字符集
mysql_query("SET NAMES 'utf8'",$db);
$sql = "select godid from sp where godid = '$_POST[godid]'";//SQL语句
$result = mysql_query($sql); //执行SQL语句
$num = mysql_num_rows($result); //统计执行结果影响的行数
if($num) //如果已经发布
{
echo "<script>alert('商品已发布'); history.go(-1);</script>";
}
else //为发布的商品
{
$sql_insert = "insert into sp (godid,godname,godtypic,godprice,godjianjie,count,sold) values('$_POST[godid]','$_POST[godname]','$_POST[godtypic]','$_POST[godprice]','$_POST[godjianjie]','','')";
$res_insert = mysql_query($sql_insert);
//$num_insert = mysql_num_rows($res_insert);
if($res_insert)
{
echo "<script>alert('发布成功!'); history.go(-1);</script>";
}
else
{
echo "<script>alert('发布失败!'); history.go(-1);</script>";
}
}
}
?> <form action=" " method="post">
<div id="neirong">
<p class="minititle"><strong>发布商品</strong></p> <p><span>类 别:</span><input name="godtypic" type="text" style="width:60px; height:22px"></p>
<p><span>商品名:</span><input name="godname" type="text" style="width:100px; height:22px">
<span>编 号:</span><input name="godid" type="text" style="width:60px; height:22px">
<span>价 格:</span><input name="godprice" type="text" style="width:80px; height:22px"></p>
<p>简 介:</p>
<p> <textarea name="godjianjie" style="width:350px; height:66px"></textarea>
</p>
<p><input type="Submit" name="Submit" value="提 交" class="submit-btn"/></p></div>
</form>
代码如下
<?php
error_reporting(0);
header("Content-Type:text/html;charset=utf-8");
if(isset($_POST["Submit"]) && $_POST["Submit"] == "提 交")
{
mysql_connect("localhost","root",""); //连接数据库
mysql_select_db("xt"); //选择数据库
mysql_query("set names 'gdk'"); //设定字符集
mysql_query("SET NAMES 'utf8'",$db);
$sql = "select godid from sp where godid = '$_POST[godid]'";//SQL语句
$result = mysql_query($sql); //执行SQL语句
$num = mysql_num_rows($result); //统计执行结果影响的行数
if($num) //如果已经发布
{
echo "<script>alert('商品已发布'); history.go(-1);</script>";
}
else //为发布的商品
{
$sql_insert = "insert into sp (godid,godname,godtypic,godprice,godjianjie,count,sold) values('$_POST[godid]','$_POST[godname]','$_POST[godtypic]','$_POST[godprice]','$_POST[godjianjie]','','')";
$res_insert = mysql_query($sql_insert);
//$num_insert = mysql_num_rows($res_insert);
if($res_insert)
{
echo "<script>alert('发布成功!'); history.go(-1);</script>";
}
else
{
echo "<script>alert('发布失败!'); history.go(-1);</script>";
}
}
}
?> <form action=" " method="post">
<div id="neirong">
<p class="minititle"><strong>发布商品</strong></p> <p><span>类 别:</span><input name="godtypic" type="text" style="width:60px; height:22px"></p>
<p><span>商品名:</span><input name="godname" type="text" style="width:100px; height:22px">
<span>编 号:</span><input name="godid" type="text" style="width:60px; height:22px">
<span>价 格:</span><input name="godprice" type="text" style="width:80px; height:22px"></p>
<p>简 介:</p>
<p> <textarea name="godjianjie" style="width:350px; height:66px"></textarea>
</p>
<p><input type="Submit" name="Submit" value="提 交" class="submit-btn"/></p></div>
</form>
分明是设置成 gdk 这个不存在的字符集,MySQL 是不认账的虽然接下来有 mysql_query("SET NAMES 'utf8'",$db);
但数据库连接资源 $db 并不存在,自然也是无效的了
mysql> SHOW VARIABLES LIKE 'collation_%';
mysql_select_db("xt"); //选择数据库
mysql_query("set names utf8"); //设定字符集 只要这三句
另外请确保你的程序是按 utf-8 字符集保存的