How to insert into database with PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:27:20
Original
957 people have browsed it

How to insert into database with PHP

$ostype=$_POST['ostype'];

$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'=>'Failed to connect to database');

 }

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'=>'Inserted into database successfully');

 }

else {

$ret_json=array('code'=>1002, 'message'=>'Insertion into database failed');

 }

 }

$jobj=new stdclass();

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

 $jobj->$key=$value;

 }

echo ''.json_encode($jobj);

 ?>

Why did the insertion into the database fail??

ID is an auto-incremented primary key, LASTDATE is a DATE type

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

What is the error message?

If LASTDATE is of DATE type $nowtime=date(‘Y-m-d’);

If LASTDATE is of 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--------------------

Quote:

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 one is the quotation marks problem as mentioned above

Second, your time() is the returned timestamp, which does not correspond to the date type. It needs to be converted and processed

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/820403.htmlTechArticleHow to insert PHP into the database $ostype=$_POST['ostype']; $uuid=$_POST['uuid' ]; $nowtime=time(); $username='XXXX'; $userpass='XXXX'; $dbhost='localhost'; $dbdatabase='XXX'; //Generate a connection...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template