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