query($sql); i"/> query($sql); i">
Home >Backend Development >PHP Tutorial >哪位大神帮小弟我看看$id = mysql_insert_id();获取不到刚插入数据的ID
哪位大神帮我看看$id = mysql_insert_id();获取不到刚插入数据的ID
$sql="SELECT * FROM `imuser` where account ='$_POST[account]' LIMIT 1"; $db->query($sql); if($db->fetchRow()){echo "{\"statusCode\":\"300\",\"message\":\"错误!用户已存在!\",\"navTabId\":\"\",\"callbackType\":\"forward\"}";exit();} $sql="INSERT INTO `imuser` (`account` ,`name` ,`password` ,`salt` ,`sex` ,`show`,`permission`,`createtime` ) VALUES ( '$_POST[account]','$_POST[name]', '$password', '$salt', '$sex', '$show','$permission',`createtime`);"; $uid = mysql_insert_id();
$sql="INSERT INTO `imdeptuser` (`did` ,`uid`) VALUES ('$did','$uid');"; $sql="INSERT INTO `imclubuser` (`cid` ,`uid`) VALUES ('$cid','$uid');"; if($db->query($sql)){echo success($msg);}else{echo success($msg);}
exit;
}
获取不到UID啊 哪位大神帮我看一下
------解决方案--------------------
$uid = mysql_insert_id();这句是获得上一步INSERT 操作产生的ID...
你这行代码前面 有定义一个INSERT插入的sql语句,可是并未去执行.....因此获取不到ID
解决办法:
$sql="INSERT INTO `imuser` (`account` ,`name` ,`password` ,`salt` ,`sex` ,`show`,`permission`,`createtime` )
VALUES ( '$_POST[account]','$_POST[name]', '$password', '$salt', '$sex', '$show','$permission',`createtime`);";
这里加上 :$db->query($sql);
$uid = mysql_insert_id();
------解决方案--------------------
$sql="SELECT * FROM `imuser` where account ='$_POST[account]' LIMIT 1";
$db->query($sql);
if($db->fetchRow()){echo "{\"statusCode\":\"300\",\"message\":\"错误!用户已存在!\",\"navTabId\":\"\",\"callbackType\":\"forward\"}";exit();}
$sql="INSERT INTO `imuser` (`account` ,`name` ,`password` ,`salt` ,`sex` ,`show`,`permission`,`createtime` )
VALUES ( '$_POST[account]','$_POST[name]', '$password', '$salt', '$sex', '$show','$permission',`createtime`);";
$db->query($sql);
$uid = mysql_insert_id();
$sql="INSERT INTO `imdeptuser` (`did` ,`uid`) VALUES ('$did','$uid');";
$sql="INSERT INTO `imclubuser` (`cid` ,`uid`) VALUES ('$cid','$uid');";
------解决方案--------------------
可以看看你插入语句的数据表是否有记录增加;如果没有,可以去看数据库的错误日志(可能需要手动打开)。