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
$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;&pound;&Oslash;&sup3;&Ugrave;

数据库整理:utf8_general_ci

<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');

$host       =   "localhost"; // your mysql server address
$user       =   "z*******"; // your mysql username
$pass       =   "m********"; // your mysql password
$tablename  =   "z*******"; // 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"));
?>

删除后

$posts[$key] = filter($value); " 来自 header.php

输出从:到:& Oslash; & 磅; &Oslash; ³& 乌格雷夫

至:Ù Ù Ù


头文件:http: //zwdha.com/header.txt

4

3 回答 3

0

如果您的数据库是完整的 utf8,您可能需要添加一些 ini_set 语句:http ://www.polak.ro/php-mysql-utf-8.html

于 2013-02-09T00:01:21.677 回答
0

这是一个非常基本的插入查询,用于查看是否可以使用 PHP 在数据库中插入数据。

<?php

function inserting_data() {
    $host = "host";
    $user = "username";
    $password = "password";
    $database = "database";
    $charset = "utf8";

    $link = mysqli_connect($host, $user, $password, $database);
    mysql_set_charset($charset, $link);
    IF (!$link) {
        echo('Unable to connect to the database!');
    } ELSE {
        /*
         * this just to test if something gets inserted into your table. 
         * If some of your columns are set to not null you should add them tot this insert query.
         */

        $query = "INSERT INTO facebook (`title`) VALUES('test_title'), ('test_title1')";
        mysqli_query($link, $query);
    }
    mysqli_close($link);
}

?>

这是一个非常基本的选择查询,用于从数据库中检索数据。

<?php

function selecting_data(){

    $host = "host";
    $user = "username";
    $password = "password";
    $database = "database";
    $charset = "utf8";

    $link = mysqli_connect($host, $user, $password, $database);
    mysql_set_charset($charset, $link);
    IF (!$link) {
        echo('Unable to connect to the database!');
    } ELSE {
        /*
         * this just to test if something gets returned from your table. 
         */

        $query = "SELECT * FROM facebook";
        $result = mysqli_query($link, $query);
        while ($rows = mysqli_fetch_array($result, MYSQLI_BOTH)){
            echo $rows['title']; 
        }
    }
    mysqli_close($link);


}
?>

我的建议是首先测试您是否可以在数据库中获取数据并检索它。通过反复试验,您将需要解决您的问题。顺便说一句,我使用了 MYSQLI_ 函数而不是 MYSQL。mysql_ 函数将很快贬值。希望这可以帮助。

于 2013-02-09T00:53:22.267 回答
0

a - 你不应该使用 mysql_ 函数。使用 PDO。这个错误是原因之一。

b - mysql_set_charset('utf8');

c - 您尝试使用 utf8_general_ci 插入数据的列?

d - 在 my.cf 上设置默认字符集和字符集服务器

如果这不起作用,请阅读: http ://www.oreillynet.com/onlamp/blog/2006/01/turning_mysql_data_in_latin1_t.html

这: http ://www.phpwact.org/php/i18n/utf-8

如果在那之后您仍然无法正确使用,请确保听取人们的意见并使用 PDO。

于 2013-02-09T01:21:03.607 回答