PHP inserting data to database garbled problem
PHP code
!--code highlighting produced by Actipro Codehighlig Hter (freeware) http://www. Codehighlighter.com/-->book Entry result bookentry result
!--? phpheader ("content-type:text/html;charset= Utf-8 "); $isbn =$_post[' ISBN ']; $author =$_post[' author '); $title =$_post[' title ']; $price =$_post[' price '];if (! ( $ISBN | | $author | | $title | | $price) {echo "Please confirm that each line has input data"; Exit;} if (!GET_MAGIC_QUOTES_GPC ()) {$ISBN =addslashes ($ISBN); $author =addslashes ($author); $title =addslashes ($title); $price =addslashes ($price);} mysql_query (' Set names UTF8 '); @ $db =new mysqli (' localhost ', ' root ', ' root ', ' books '); if (Mysqli_connect_error ()) {echo " Link database not successful "; Exit;} Header ("Content-type:text/html;charset=utf-8"); $query = "INSERT Into Books (ISBN, author, title, price) VALUES ('". $ISBN . "', '". $author. "', '". $title. "', '". $price. "')"; $result = $db--->query ($query), if ($result) {echo $db->affected_rows. "The book data entry was successful. ";} Else{echo "Data entry failed. ";}?
I have used the header () to turn the PHP page into Utf-8
Both the database and the table are encoded UTF-8
------Solution--------------------
discuss
mysql_query (' Set names UTF8 '); And Mysqli does not match the tune, should be $db->query (' Set names UTF8 ');
Where did you see the inserted data garbled? phpMyAdmin?
------Solution--------------------
I have encountered the program encoding is normal, the page display is normal, but phpmyadmin in the case of garbled. That's phpMyAdmin's configuration, but I don't know how to fix it. You can write a simple applet to read the database contents to the page display to see
------Solution--------------------
The program code is not a problem.
------Solution--------------------
The Mysql_connnet (...) should be executed first, and the database is linked to mysql_query ("Set names Utf-8").