0

我有 php & mysql 的问题,使用 utf-8 插入数据库。第一个文件: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 :

<?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!";
}
?>

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

Oslash;£Ø³Ù

4

1 回答 1

0

3 解决方案在您的情况下...

首先,您可以在 UTF-8 字符集中创建数据库,不需要转换。

其次,您可以测试此代码...

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

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

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));
于 2013-02-07T17:01:30.463 回答