php: Insert special characters into myadmin databa

2019-08-20 11:35发布

Hello stackoverflow people!

I've got a problem which I haven't been able to solve after 3 months of trying. There are similar problems described on this website, but they didn't give me the result I need.

The result I get: click here for the result i get

structure setting: click here for the structure

My coding:

include 'connectiondetails.php';

//making the database connection
dbVerbind();

$str = '<p>€ é í å </p>';

// things i've tried
//$test1 = $_GET['test1'];
//$test2 = htmlspecialchars_decode('<p>€ é í å </p>');
//$test3 = html_entity_decode($str, ENT_QUOTES, 'UTF-8');
//$test4 = htmlspecialchars_decode( $str , ENT_QUOTES);
//$test5 = htmlspecialchars('<p>€ é í å </p>', ENT_QUOTES);
//$test6 = parse_str(html_entity_decode($str), ENT_QUOTES);
//$orig = "<p>€ é í å </p>";
//$a = htmlentities($orig);
//$test7 = html_entity_decode($a);
//$test8 = $_GET['test8'];
//$test9 = htmlentities($str, ENT_QUOTES);
//$test10 = mysql_real_escape_string('€ é í å');

$test1 = $_GET['test1'];
$test2 = $_GET['test2'];
$test3 = $_GET['test3'];
$test4 = $_GET['test4'];
$test5 = $_GET['test5'];
$test6 = $_GET['test6'];
$test7 = $_GET['test7'];
$test8 = $_GET['test8'];
$test9 = $_GET['test9'];
$test10 = $_GET['test10'];

if (empty( $test1 )) {
echo '<p>empty</p>';
}
else {
$results =  mysql_query( "  INSERT INTO insert_test.content (   
                                            test1,
                                            test2,
                                            test3,
                                            test4,
                                            test5,
                                            test6,
                                            test7,
                                            test8,
                                            test9,
                                            test10
                                            )
                    VALUES(     '$test1',
                                '$test2',
                                '".htmlspecialchars('€ é í å')."',
                                '".mysql_real_escape_string('€ é í å')."',
                                '€ é í å',
                                '€ é í å',
                                '$test7',
                                '$test8',
                                '$test9',
                                '$test10'
                              )
                    " );
}

1条回答
Explosion°爆炸
2楼-- · 2019-08-20 12:08

You are sending characters as Unicode, and the fields are in Unicode, but the database connection is not. Use this immediately after connecting:

SET NAMES 'utf8'
查看更多
登录 后发表回答