- 相關(guān)推薦
PHP怎么插入數(shù)據(jù)庫(kù)
導(dǎo)語(yǔ):PHP這么鏈接數(shù)據(jù)庫(kù)呢?以下的是百分網(wǎng)小編為大家搜集PHP插入數(shù)據(jù)庫(kù)的方法,希望對(duì)你有所幫助。
$ostype=$_POST['ostype'];
$uuid=$_POST['uuid'];
$nowtime=time();
$username='XXXX';
$userpass='XXXX';
$dbhost='localhost';
$dbdatabase='XXX';
//生成一個(gè)連接
$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'=>'鏈接數(shù)據(jù)庫(kù)失敗');
}
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'=>'插入數(shù)據(jù)庫(kù)成功');
}
else {
$ret_json=array('code'=>1002, 'message'=>'插入數(shù)據(jù)庫(kù)失敗');
}
}
$jobj=new stdclass();
foreach($ret_json as $key=>$value){
$jobj->$key=$value;
}
echo ''.json_encode($jobj);
?>
為嘛插入數(shù)據(jù)庫(kù)失敗呢??
ID是自增的主鍵,LASTDATE是DATE類型
------解決方案--------------------
報(bào)錯(cuò)提示什么?
如果LASTDATE是DATE類型 $nowtime=date(‘Y-m-d’);
如果LASTDATE是DATETIME類型$nowtime=date(‘Y-m-d H:i:s');
------解決方案--------------------
echo mysql_error(); 報(bào)什么錯(cuò)誤沒(méi)有
------解決方案--------------------
少了引號(hào)了。
$result = mysql_query("INSERT INTO t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, '$ostype', '$uuid', '$nowtime')");
------解決方案--------------------
引用:
少了引號(hào)了。
$result = mysql_query("INSERT INTO t_dblocal_userinformation (ID, OSTYPE, UUID, LASTDATE) VALUES (NULL, '$ostype', '$uuid', '$nowtime')");同意
------解決方案--------------------
第一個(gè) 想樓上那樣說(shuō)的 引號(hào)的問(wèn)題
第二個(gè) 你的time()是返回的時(shí)間戳,和date類型對(duì)應(yīng)不上吧 要轉(zhuǎn)化處理一下才行
【PHP怎么插入數(shù)據(jù)庫(kù)】相關(guān)文章:
PHP大批量插入數(shù)據(jù)庫(kù)的方法01-05
PHP大批量插入數(shù)據(jù)庫(kù)的3種方法05-23
PHP訪問(wèn)數(shù)據(jù)庫(kù)04-27