Issue with PHP MySQL and insert into UTF-8 [closed

2020-04-15 11:24发布

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

second file , plugin 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!";
}
?>

when i write arabic language in the form and submit , it went to database with unkown language like :

Oslash;£Ø³Ù

标签: php mysql utf-8
3条回答
趁早两清
2楼-- · 2020-04-15 11:56

I had similar problems with Greek characters. Maybe they look unfamiliar in the database, but they were correct.

Try to read back the values in PHP and do an echo. It will show if it was stored correctly.

查看更多
三岁会撩人
3楼-- · 2020-04-15 11:57

There are two things to help make this easier. First, remember to have your database in UTF-8 format. Second, make sure to send the UTF-8 content-type with your page.

For MySQL, if you already have the database created, do this:

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

For the table, alter it in this fashion:

ALTER TABLE table_name DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Then, send the proper content-type with your document. Use this HTML tag:

<meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
查看更多
倾城 Initia
4楼-- · 2020-04-15 12:17

Try the following:

  1. Add UTF-8 meta tag: <meta http-equiv="Content-type" value="text/html; charset=UTF-8" />
  2. Add UTF-8 header: header('Content-Type: text/html; charset=utf-8'); in case the tag will be ignored with lack of proper markup
  3. Make sure your script is encoded in UTF-8 (use Notepad++ for that)
  4. Run set names 'utf8' query before inserting
查看更多
登录 后发表回答