How to deal with database insertion-php Tutorial

Source: Internet
Author: User
Database insertion & lt ;? Php $ connmysql_connect (& quot; localhost & quot;, & quot; root & quot;, & quot; 1234567 & quot;); mysql_query (& quot; SETNAMESutf8 & quot ;); if (! $ Conn) & nbsp ;{& nbsp; die (cocould database insertion

$ Conn = mysql_connect ("localhost", "root", "1234567 ");
Mysql_query ("set names 'utf8 '");
If (! $ Conn)
{
Die ('could not connect: '. mysql_error ());
}
Mysql_select_db ("code", $ conn );

$ SQL = "INSERT INTO keycode (key) VALUES ('$ _ POST [username]')";
If ($ _ POST [username]! = Null ){
Mysql_query ($ SQL, $ conn );
Echo "status = '$ _ POST [username]'";
} Else
Echo "no number! ";
Mysql_close ();
?>

Why can't I insert a parameter sent from the flex end ???
Echo "status = '$ _ POST [username]'" can be displayed. However, nothing is inserted in the database !!!!
Thank you !!!!!


------ Solution --------------------
It may be that the passed Chinese characters cannot be inserted normally. Try to use English. If possible, perform iconv transcoding in Chinese.
------ Solution --------------------
Mysql_query ($ SQL, $ conn) or die (mysql_error (); // check whether the error is correct.

Echo mysql_affected_rows ($ conn); // Check the number of entries in and out.
------ Solution --------------------
Is this not an error?
The key is a reserved mysql word, which must be escaped during other operations.

$ SQL = "INSERT INTO keycode ('key') VALUES ('$ _ POST [username]')";
------ Solution --------------------
I have made this mistake before.
------ Solution --------------------
Discussion
No !!! This is depressing.
I output the $ SQL statement like this: INSERT INTO keycode (key) VALUES ('17 ')
Then I run the prompt in mysql:
#1064-You have an error in your SQL syntax; check the manual that corresponds to your MySQL server vers ......

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.