PHP脚本将UTF8插入MYSQL [英] Php script Inserting UTF8 TO MYSQL

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

问题描述

我对php& amp;有问题mysql,使用utf-8插入数据库. 第一个文件: 附加网站:

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';
  ?>    

第二个文件,插件addsite.php:

second file , plugin addsite.php :

<?php
header('Content-Type: text/html; charset=utf-8');
mysql_query("SET NAMES utf8");
$num1 = mysql_query("SELECT * FROM `digg` WHERE `url`='{$posts['url']}'");
mysql_query("SET NAMES utf8");
$num = mysql_num_rows($num1);
mysql_query("SET NAMES utf8");
if($num > 0){
$error = "Page already added!";
}else if(!strstr($posts['url'], 'digg.com')) {
$error = "Incorrect URL! You must include 'digg.com'";
}else{
mysql_query("SET NAMES utf8");
   mysql_query("INSERT INTO `digg` (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;£Ø³Ù

Oslash;£Ø³Ù

推荐答案

3种针对您的解决方案...

3 Solutions in your case...

首先,您可以使用UTF-8字符集创建数据库,不需要进行转换.

First, you can create your database in the UTF-8 charset, conversions is not needed.

第二,您可以测试此代码...

Second, you can test this code...

htmlentities(urldecode( $my_var_to_utf8 ), ENT_QUOTES, 'UTF-8');

如果它不起作用,请尝试使用此功能...

If it don't works, try this function...

function to_utf8( $string ) {
        // From http://w3.org/International/questions/qa-forms-utf-8.html
        if ( preg_match('%^(?:
                [\x09\x0A\x0D\x20-\x7E]              # ASCII
                | [\xC2-\xDF][\x80-\xBF]             # non-overlong 2-byte
                | \xE0[\xA0-\xBF][\x80-\xBF]         # excluding overlongs
                | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}  # straight 3-byte
                | \xED[\x80-\x9F][\x80-\xBF]         # excluding surrogates
                | \xF0[\x90-\xBF][\x80-\xBF]{2}      # planes 1-3
                | [\xF1-\xF3][\x80-\xBF]{3}          # planes 4-15
                | \xF4[\x80-\x8F][\x80-\xBF]{2}      # plane 16
        )*$%xs', $string) ) {
        return $string;
        } else {
            return iconv('CP1252', 'UTF-8', $string);
        }
    }

要使用...

echo(to_utf8($my_var_to_utf8));

这篇关于PHP脚本将UTF8插入MYSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆