How to insert a PHP database _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
How to insert PHP into the database. How to insert a database in PHP $ ostype $ _ POST [ostype]; $ uuuuid]; $ nowtimetime (); $ usernameXXXX; $ userpassXXXX; $ dbhostlocalhost; $ dbdatabaseXXX; how to insert a PHP connection to a database

  

$ Ostype =$ _ POST ['osstype'];

$ Uuid = $ _ POST ['uuid'];

$ Nowtime = time ();

$ Username = 'xxxx ';

$ Userpass = 'xxxx ';

$ Dbhost = 'localhost ';

$ Dbdatabase = 'XXX ';

// Generate a connection

$ Db_connect = mysql_connect ($ dbhost, $ username, $ userpass) or die ("Unable to connect to the MySQL! ");

$ Ret_json;

If (! $ Db_connect ){

$ Ret_json = array ('code' => 1001, 'message' => 'Database connection failed ');

}

Else {

Mysql_select_db ($ dbdatabase, $ db_connect );

$ Result = mysql_query ("insert into t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, $ ostype, $ uuid, $ nowtime )");

If ($ result ){

$ Ret_json = array ('code' => 1000, 'message' => 'Database inserted successfully ');

}

Else {

$ Ret_json = array ('code' => 1002, 'message' => 'Database insertion failed ');

}

}

$ Jobj = new stdclass ();

Foreach ($ ret_json as $ key => $ value ){

$ Jobj-> $ key = $ value;

}

Echo ''. json_encode ($ jobj );

?>

Why does the database insertion fail ??

ID is the auto-increment primary key and LASTDATE is the DATE type.

------ Solution --------------------

What is the error message?

If LASTDATE is of the DATE Type $ nowtime = date ('Y-m-D ');

If LASTDATE is of the DATETIME type $ nowtime = date ('Y-m-d H: I: s ');

------ Solution --------------------

Echo mysql_error (); no error reported

------ Solution --------------------

The quotation marks are missing.

$ Result = mysql_query ("insert into t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, '$ ostype', '$ uuid', '$ nowtime ')");

------ Solution --------------------

Reference:

The quotation marks are missing.

$ Result = mysql_query ("insert into t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, '$ ostype', '$ uuid', '$ nowtime ')"); agree

------ Solution --------------------

The first question about the quotation marks that I want to say above

Second, your time () is the returned timestamp. if it does not match the date type, you have to convert it for processing.

Dimensions $ ostype =$ _ POST ['osstype']; $ uuid =$ _ POST ['uuid']; $ nowtime = time (); $ username = 'xxxx '; $ userpass = 'xxxx'; $ dbhost = 'localhost'; $ dbdatabase = 'XXX'; // Generate a connection...

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.