将UTF8插入MYSQL的Php脚本

将UTF8插入MYSQL的Php脚本,php,mysql,utf-8,Php,Mysql,Utf 8,我有一个php和mysql的问题,使用utf-8插入数据库。 第一个文件: 地址: <?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'] == ""){ $err

我有一个php和mysql的问题,使用utf-8插入数据库。 第一个文件: 地址:

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

  • 从下拉菜单中选择要升级的exchange类型
  • 设置您希望为每位用户提供的完整硬币数量(CPC)
  • 硬币数量越高,链接位置越高
  • 添加站点 错误: 成功: 警告: 键入


    链接
    第二个文件,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!";
    }
    ?>
    

    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));
    

    尽管有强制要求,但不要使用mysql_uu扩展,因为它已被弃用,并且存在其他安全问题;您似乎已经设置了PHP字符集和MySQL表字符集,但还没有在连接器
    MySQL\u set\u charset()
    上设置字符集,这将始终在工作中使用扳手。表本身的文本排序规则是什么?如果您将文本存储到非utf8的内容中,那么拥有utf8连接是不够的;mysql_查询(“设置名称‘utf8’”);mysql_查询(“设置字符集utf8”);mysql_查询(“设置排序规则_连接='utf8\U unicode\U ci'”;mysql_查询(“插入
    digg
    (用户、url、标题、cpc)值(“{$data->id}”、“{$posts['url']}”、“{$posts['title']}”、“{$posts['cpc']}”)$success=“页面添加成功!”;正如@MarcB所说,问题可能不在于代码本身,而在于数据库表——看看数据库表本身的字符集/排序规则;所有3个位置都需要utf-8字符集,PHP/HTML(文档头)、数据库连接器和数据库表本身。