2

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

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

Oslash;£Ø³Ù

4

3 回答 3

0

尝试以下操作:

  1. 添加 UTF-8 元标记: <meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
  2. 添加 UTF-8 标头: header('Content-Type: text/html; charset=utf-8');以防标记因缺少正确标记而被忽略
  3. 确保您的脚本以 UTF-8 编码(为此使用 Notepad++)
  4. set names 'utf8'插入前运行查询
于 2013-02-07T15:29:50.560 回答
0

有两件事可以帮助使这更容易。首先,请记住将您的数据库设为 UTF-8 格式。其次,确保将 UTF-8 内容类型与您的页面一起发送。

对于 MySQL,如果您已经创建了数据库,请执行以下操作:

ALTER DATABASE database_name CHARACTER SET utf8 DEFAULT CHARACTER SET utf8
    COLLATE utf8_general_ci DEFAULT COLLATE utf8_general_ci;

对于表,以这种方式改变它:

ALTER TABLE table_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

然后,将正确的内容类型与您的文档一起发送。使用这个 HTML 标记:

<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
于 2013-02-07T15:24:01.520 回答
0

我对希腊字符也有类似的问题。也许他们在数据库中看起来不熟悉,但他们是正确的。

尝试读回 PHP 中的值并进行回显。它将显示它是否正确存储。

于 2013-02-07T15:12:20.220 回答