Php stores data to the database. The echo SQL statement can be executed normally in mysql but is executed incorrectly in php.

Source: Internet
Author: User
Php stores data to the database. The echo SQL statement can be executed normally in mysql but is executed incorrectly in php.
$ Title = $ _ POST ["title"];
$ Metact_t = $ _ POST ["metact_t"];
$ Acter = $ _ SESSION ["user"];
$ Department = $ _ POST ["department"];
$ Metinfo = $ _ POST ["metinfo"];
$ Jb = $ _ POST ["jb"];
$ Department_t = $ _ POST ["department_t"];
The passed parameters are obtained and the output test is complete.
$ Title = iconv ("UTF-8", "gbk", strip_tags ($ title ));
$ Metact_t = iconv ("UTF-8", "gbk", strip_tags ($ metact_t ));
$ Acter = iconv ("UTF-8", "gbk", strip_tags ($ acter ));
$ Department = iconv ("UTF-8", "gbk", strip_tags ($ department ));
$ Metinfo = iconv ("UTF-8", "gbk", strip_tags ($ metinfo ));
$ Jb = iconv ("UTF-8", "gbk", strip_tags ($ jb ));
$ Status = iconv ("UTF-8", "gbk", 'unsolved ');
$ Department_t = iconv ("UTF-8", "gbk", strip_tags ($ department_t ));
$ SaveMeetingSql = "insert into t_metact (id, title, metact_t, acter, department, metinfo, jb, department_t, status )".
"Values ('$ ID',' $ title', '$ metact_t', '$ acter', '$ department', '$ metinfo', '$ jb ', '$ department_t', '$ status ')";
Echo ($ saveMeetingSql );
The transcoding output SQL statement is as follows:
Insert into t_metact (id, title, metact_t, acter, department, metinfo, jb, department_t, status) values ('2', 'Test 100', '2017-09-09 ', 'admin', 'network amount', 'ceshi', 'weight', 'network amount', 'unsolved ')
Can be inserted normally in SQL manager
Error output in php: ncorrect string value: '\ xB2 \ xE2 \ xCA \ xd409... 'For column 'title' at row 1 means that the test 0908 corresponding to the title is not in the normal character format, and the database design field is varchar 20 in length. I don't know where the problem is, hope you can give me some advice


Reply to discussion (solution)

You send a gbk encoded string to the database, and the default character set of the database is not gbk.
Therefore, the database cannot identify the input string, so an error occurs.

What is your database code?
The inserted data must be consistent with the data encoding.

I guess it's also about encoder.

You send a gbk encoded string to the database, and the default character set of the database is not gbk.
Therefore, the database cannot identify the input string, so an error occurs.


The echo SQL code I transcoded is normal, that is, I don't know how to send it to the database as gbk. it should be a coding problem, but I just don't know where the problem is, the UTF-8 code I set when connecting to the database

What is your database code?
The inserted data must be consistent with the data encoding.


The database is utf8, and I have all the inserted SQL parameters. The echo SQL execution is fine.

1. there is no database operation function in your code, so I don't know what database you are using. Therefore, no suggestions are provided in the reply.
2. you have performed operations such as $ title = iconv ("UTF-8", "gbk", strip_tags ($ title) on the incoming data, this converts UTF-8 encoding to gbk encoding.
3. print \ xB2 \ xE2 \ xCA \ xd409... in the error message in php.
Echo "\ xB2 \ xE2 \ xCA \ xd409 ...";
Is the gbk encoding test 09...
4. if utf8 has been declared when you connect to the database, the gbk encoding data will report an error because it is not within the UTF-8 range.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.