问题与 Utf8 并插入到 mysql [英] ISSUE With Utf8 and inserting to mysql

查看:65
本文介绍了问题与 Utf8 并插入到 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 php 时遇到问题 &mysql,使用 utf-8 插入到数据库中.第一个文件:添加站点:

 $value) {$posts[$key] = filter($value);}if(isset($posts['type'])){if($posts['url'] == "http://" || $posts['url'] == ""){$error = "添加你的页面链接!";}else if($posts['title'] == ""){$error = "添加页面标题!";}else if(!preg_match("/\bhttp\b/i", $posts['url'])){$error = "URL 必须包含 http://";}else if(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $posts['url'])){$error = "请不要在网址中使用特殊字符.<";}别的{包括 "plugins/" .$posts['type'] ."/addsite.php";}}?><div class="contentbox"><font size="2"><li>从下拉菜单中选择您要推广的交易所类型.</li><li>设置您希望为每位用户提供的硬币数量 (CPC).</li><li>硬币数量越高,链接位置就越高.</li>

<div class="contentbox"><div class="head">添加站点</div><div class="contentinside"><?php if(isset($error)) { ?><div class="error">错误:<?php echo $error;?></div><?php }if(isset($success)) { ?><div class="success">成功:<?php echo $success;?></div><?php }if(isset($warning)) { ?><div class="warning">警告:<?php echo $warning;?></div><?php } ?><form class="contentform" method="post">类型<br/><select name="type"><?php $select = hook_filter('add_site_select', "");回声 $select;?></选择><br/><br/>链接
<input name="url" type="text" value="<?php if(isset($posts["url"])) { echo $posts["url"]; } ?>"/><br/><br/>标题<br/><input name="title" type="text" value="<?php if(isset($posts["title"])) { echo $posts["title"]; } ?>"/><br/><br/>每次点击费用<br/><?php if($data->premium > 0) { ?><select name="cpc"><?php for($x = 2; $x <= $site->premcpc; $x++) { if(isset($posts["cpc"]) && $posts["cpc"] == $x) { echo "<option selected>$x</option>";} else { echo "<option>$x</option>";} } ?></select><br/><br/><?php }else{ ?><select name="cpc"><?php for($x = 2; $x <= $site->cpc; $x++) { if(isset($posts["cpc"]) && $posts["cpc"] == $x) { echo "<option selected>$x</option>";} else { echo "<option>$x</option>";} } ?></select><br/><br/><?php } ?><input style="width:40%;"类型=提交"/></表单>

<?php}别的{echo "请登录查看此页面!";}包括'footer.php';?>

第二个文件,插件 addsite.php

 0){$error = "页面已添加!";}else if(!strstr($posts['url'], 'facebook.com')) {$error = "URL 不正确!你必须包含'facebook.com'";}别的{mysql_query($qry);mysql_query("INSERT INTO `facebook` (user, url, title, cpc) VALUES('{$data->id}', '{$posts['url']}', '{$posts['title']}', '{$posts['cpc']}')");$success = "页面添加成功!";}?>

当我在表单中写下阿拉伯语并提交时,它使用未知语言进入数据库,例如:

 &Oslash;&pound;&Oslash;&sup3;&Ugrave;

数据库整理:utf8_general_ci

配置文件

$host = "localhost";//你的mysql服务器地址$user = "";//你的mysql用户名$pass = "";//你的mysql密码$tablename = "";//你的mysql表session_start();$数据=空;if(!(@mysql_connect("$host","$user","$pass") && @mysql_select_db("$tablename"))) {?>数据库错误<?出口;}include_once 'functions.php';require_once "includes/pluggable.php";foreach( glob("plugins/*/index.php") as $plugin) {require_once($plugin);}hook_action('初始化');$site = mysql_fetch_object(mysql_query("SELECT * FROM settings"));?>

解决方案

将表格的整理和字符集更改为utf8

alter table 转换为字符集 utf8 整理 utf8_unicode_ci;

I have a problem with php & mysql, insert to database using utf-8. first file: addsite:

<?php
include 'header.php';
if(isset($data)) {
foreach($_POST as $key => $value) {
$posts[$key] = filter($value);
}
if(isset($posts['type'])){
if($posts['url'] == "http://" || $posts['url'] == ""){
$error = "Add your page link!";
}else if($posts['title'] == ""){
$error = "Add your page title!";
}else if(!preg_match("/\bhttp\b/i", $posts['url'])){
$error = "URL must contain http://";
}else if(!preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i',       $posts['url'])){
$error = "Please do not use special characters in the url.<";
}else{
    include "plugins/" . $posts['type'] . "/addsite.php";
}
}
?>
<div class="contentbox">
<font size="2">
<li>Pick the type of exchange you are promoting from the dropdown menu.</li>
<li>Set the amount of coins you wish to give per user complete(CPC).</li>
<li>The higher the amount of coins the higher the Links position.</li>
</div>
<div class="contentbox">
<div class="head">Add Site</div>
<div class="contentinside">
    <?php if(isset($error)) { ?>
    <div class="error">ERROR: <?php echo $error; ?></div>
    <?php }
    if(isset($success)) { ?>
    <div class="success">SUCCESS: <?php echo $success; ?></div>
    <?php }
    if(isset($warning)) { ?>
    <div class="warning">WARNING: <?php echo $warning; ?></div>
    <?php } ?>

    <form class="contentform" method="post">
        Type<br/>
        <select name="type"><?php $select = hook_filter('add_site_select', ""); echo   $select; ?></select><br/><br/>
        Link<br/>
        <input name="url" type="text" value="<?php if(isset($posts["url"])) { echo $posts["url"]; } ?>"/><br/><br/>
        Title<br/>
        <input name="title" type="text" value="<?php if(isset($posts["title"])) { echo $posts["title"]; } ?>"/><br/><br/>
        Cost Per Click<br/>
        <?php if($data->premium > 0) { ?>
        <select name="cpc"><?php for($x = 2; $x <= $site->premcpc; $x++) { if(isset($posts["cpc"]) && $posts["cpc"] == $x) { echo "<option selected>$x</option>"; } else { echo "<option>$x</option>"; } } ?></select><br/><br/>
        <?php }else{ ?>
        <select name="cpc"><?php for($x = 2; $x <= $site->cpc; $x++) { if(isset($posts["cpc"]) && $posts["cpc"] == $x) { echo "<option selected>$x</option>"; } else { echo "<option>$x</option>"; } } ?></select><br/><br/>
        <?php } ?>
        <input style="width:40%;" type="Submit"/>
    </form>
</div>
 </div>
<?php
 }
else
 {
echo "Please login to view this page!";
 }
 include 'footer.php';
  ?>    

second file , plugin addsite.php

<?php
$num1 = mysql_query("SELECT * FROM `facebook` WHERE `url`='{$posts['url']}'");
$num = mysql_num_rows($num1);
if($num > 0){
$error = "Page already added!";
 }else if(!strstr($posts['url'], 'facebook.com')) {
$error = "Incorrect URL! You must include 'facebook.com'";
}else{
mysql_query($qry);
  mysql_query("INSERT INTO `facebook` (user, url, title, cpc) VALUES('{$data->id}', '{$posts['url']}', '{$posts['title']}', '{$posts['cpc']}') ");
$success = "Page added successfully!";
}
?>

when i write arabic language in the form and submit , it went to database with unkown language like :

 &Oslash;&pound;&Oslash;&sup3;&Ugrave;

database collaction : utf8_general_ci

config file

$host       =   "localhost"; // your mysql server address
$user       =   ""; // your mysql username
$pass       =   ""; // your mysql password
$tablename  =   ""; // your mysql table

session_start();
$data = null;
if(!(@mysql_connect("$host","$user","$pass") && @mysql_select_db("$tablename"))) {
    ?>
    <html>
    MSQL ERROR
    <?
    exit;
}

include_once 'functions.php';
require_once "includes/pluggable.php";
foreach( glob("plugins/*/index.php")  as $plugin) {  
  require_once($plugin);  
}  

hook_action('initialize');

$site = mysql_fetch_object(mysql_query("SELECT * FROM settings"));
?>

解决方案

change the collate and character set to utf8 for the table

alter table <some_table> convert to character set utf8 collate utf8_unicode_ci;

这篇关于问题与 Utf8 并插入到 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆