PHP获取客户端数据保存到MySQL数据库 出现中文乱码解决方案

时间:2021-04-11 07:31:13
<?php
/**
* 添加电子优惠券 联系人信息
*
1.连接数据库 $conn=mysql_connect("localhost","user","password") or die("数据库连接出错".mysql.error());
2.选择数据表 mysql_select_db("db_database",$conn) or die ("数据库访问出错".mysql.error());
3.设置数据库格式 mysql_query("set names gb2312");
4.写SQL语句 $sql="select * from tb_admin where id=$id";
5.执行SQL语句 $query=mysql_query($query);
6.获取执行结果 $info=mysql_fetch_array($query);
7.关闭记录集 mysql_free_result($info);
8.关闭记录结果 mysql_close($conn);
*/

//获取客户端数据
add();
//demo();


//添加
function add(){
/*
mobile:mobile,uname:uname,amount:amount,
intime:intime,explain:explain
$in_sql = "insert into shopnc_coupon_contacter (memberId,
contactName,mobile,couponId,couponName,bookTime,bookNum,mark,
merchantId,merchantName) values(-1,
'天街店','15123316303',-1,'电子优惠券名称','说的',1,'http://localhost:7788/jcshh/index.php?act=coupon_store&op=detail&coupon_id=5&id=6',
-1,'商户名称' )";
*/
$memberId =-1;
$contactName ='联系人姓名';
$mobile ='15123316303';
$couponId =-1;
$couponName ='电子优惠券名称';
$bookTime ='';
$bookNum =1;
$merchantId =-1;
$merchantName ='商户名称';
$mark ='暂无其他说明';

$contactName =$_POST["uname"];
$mobile =$_POST["mobile"];
$couponId =-1;
$couponName ='电子优惠券名称';
$bookTime = $_POST["intime"];
$bookNum = $_POST["amount"];
$merchantId =-1;
$merchantName ='商户名称';
$mark = $_POST["explain"];
$mark = str_replace("\'"," ",$mark);
/*
$contactName =$_GET["uname"];
$mobile =$_GET["mobile"];
$couponId =-1;
$couponName ='电子优惠券名称';
$bookTime = $_GET["intime"];
$bookNum = $_GET["amount"];
$merchantId =-1;
$merchantName ='商户名称';
$mark = $_GET["explain"];
$mark = str_replace("\'"," ",$mark);
*/
$mark = iconv("UTF-8", "UTF-8", $mark);
$couponName =iconv("UTF-8", "UTF-8", $couponName);
$couponName =iconv("UTF-8", "UTF-8", $merchantName);
$in_sql="insert into shopnc_coupon_contacter (memberId,
contactName,mobile,couponId,couponName,bookTime,bookNum,mark,
merchantId,merchantName) values($memberId,
'$contactName','$mobile',$couponId,'$couponName','$bookTime',$bookNum,'$mark',
$merchantId,'$merchantName' )";
$conn = openCon();
mysql_query("SET NAMES 'utf8'");//通知服务器客户端传递过去的语句的编码
mysql_query("SET CHARACTER_SET_CLIENT=utf8"); //服务器设置客户端编码
mysql_query("SET CHARACTER_SET_RESULTS=utf8");//设置查询结果的编码
$in_result=mysql_query($in_sql);
if(mysql_affected_rows()<1){
echo "[\"您的信息保存失败,请重试!\"]";
}else
{
echo "[\"您的信息已经保存成功!\"]";
}
mysql_close();

}
//获取数据库链接对象
function openCon(){
$conn=mysql_connect('localhost','root','root') or die ("数据库连接出错".mysql.error());
mysql_select_db("shopnc",$conn) or die ("数据库访问出错".mysql.error());
return $conn;
}

function demo(){
$conn= mysql_connect('localhost','root','root') or die('连接失败');
mysql_select_db('shopnc', $conn) or die('选择数据库失败');
mysql_query("SET NAMES utf8");
$sql="select * from shopnc_coupon_contacter";
$result=mysql_query($sql);
if($row=mysql_fetch_row($result)){
print_r($row);
}
}



只需要在执行操作前执行如下设置即可:

mysql_query("SET NAMES 'utf8'");//通知服务器客户端传递过去的语句的编码
mysql_query("SET CHARACTER_SET_CLIENT=utf8"); //服务器设置客户端编码
mysql_query("SET CHARACTER_SET_RESULTS=utf8");//设置查询结果的编码