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