• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php向数据库插入数据乱码有关问题

php 搞代码 3年前 (2022-01-24) 8次浏览 已收录 0个评论

php向数据库插入数据乱码问题

PHP code

<!---ecms Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->book entry result<body><h1>book entry result</h1><?phpheader("content-type:text/html;charset=utf-8");$isbn=$_POST['isbn'];$author=$_POST['author'];$title=$_POST['title'];$price=$_POST['price'];if(!($isbn||$author||$title||$price)){    echo"请确认每行都输入数据";    exit;}if(!get_magic_quotes_gpc()){    $isbn=addslashes($isbn);    $author=addslashes($author);    $title=addslashes($title);    $price=addslashes($price);}mysql_query('set names utf8');@$db=new mysqli('localhost','root','root',<i style="color:transparent">本#文来源gaodai$ma#com搞$$代**码网$</i><button>搞代gaodaima码</button>'books');if(mysqli_connect_error()){echo "链接数据库不成功";    exit;}header("content-type:text/html;charset=utf-8");$query="insert into books(isbn ,author ,title ,price)values('".$isbn."', '".$author."', '".$title."', '".$price."')";$result=$db->query($query);if($result){echo $db->affected_rows." 本书数据录入成功。";}else{echo"数据录入失败。";}?>

我已经用header()把php页变成utf-8
数据库和表的的编码都是utf-8

——解决方案——————–

探讨

mysql_query(‘set names utf8’); 和mysqli不搭调,应该是 $db->query(‘set names utf8’);
你是在哪儿看到的插入的数据乱码的?phpmyadmin?

——解决方案——————–
我以前遇到过程序编码正常,页面显示正常,唯独phpmyadmin中乱码的情况。那是phpmyadmin的配置没弄好,不过我也不知道怎么改。你可以写个简单的小程序把数据库内容读到页面显示看看
——解决方案——————–
程序编码是不是有问题的。
——解决方案——————–
应该先执行mysql_connnet(……),链接好数据库在执行mysql_query(“set names utf-8”);吧


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php向数据库插入数据乱码有关问题
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址