mysql_insert_id() 返回 0
我知道有很多同名的主题.但主要是查询被插入了错误的位置.但我认为我把它放对了.所以问题是,即使将数据插入数据库,我仍然得到 0.有人知道我可能错的答案吗?
I know there are a lot of topics with the same title. But mostly it's the query that's been inserted in the wrong place. But I think I placed it right. So the problem is, that I still get 0 even when the data is inserted in the db. Does someone knows an answer where I could be wrong?
这是我的代码:
mysql_query('SET NAMES utf8');
$this->arr_kolommen = $arr_kolommen;
$this->arr_waardes = $arr_waardes;
$this->tabel = $tabel;
$aantal = count($this->arr_kolommen);
//$sql="INSERT INTO `tbl_photo_lijst_zoekcriteria` ( `PLZ_FOTO` , `PLZ_ZOEKCRITERIA`,`PLZ_CATEGORIE`)VALUES ('$foto', '$zoekje','$afdeling');";
$insert = "INSERT INTO ".$this->tabel." ";
$kolommen = "(";
$waardes = " VALUES(";
for($i=0;$i<$aantal;$i++)
{
$kolommen .=$this->arr_kolommen[$i].",";
$waardes .="'".$this->arr_waardes[$i]."',";
}
$kolommen = substr($kolommen,0,-1).")";
$waardes = substr($waardes,0,-1).")";
$insert .=$kolommen.$waardes;
$result = mysql_query($insert,$this->db) or die ($this->sendErrorToMail(str_replace(" ","",str_replace("
","
",$insert))."
".str_replace(" ","",str_replace("
","
",mysql_error()))));
$waarde = mysql_insert_id();
提前非常感谢,因为我几乎已经为这个问题头疼了一整天.(可能是一些小而愚蠢的事情)
Thanks a lot in advance, because I have been breaking my head for this one for almost already a whole day. (and probably it's something small and stupid)
推荐答案
根据手册 mysql_insert_id 返回:
According to the manual mysql_insert_id returns:
上一个查询为 AUTO_INCREMENT 列生成的 ID成功,0 如果前一个查询没有生成 AUTO_INCREMENT值,如果没有建立 MySQL 连接,则为 FALSE.
The ID generated for an AUTO_INCREMENT column by the previous query on success, 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established.
由于它没有给你 false
并且不是正确的数字,它表明查询的表没有生成自动增量值.
Since it does not give you false
and not the correct number it indicates that the queried table didn't generate an auto-increment value.
我能想到两种可能:
- 您的表格没有 auto_increment 字段
- 由于您没有提供指向 mysql_insert_id() 的链接,而是使用了 mysql_query() 的链接,因此在检索最后插入的 id 时,它可能不是正确的表.
解决办法:
- 确保它有一个 auto_increment 字段
- 同时提供链接:
$waarde = mysql_insert_id($this->db);
相关文章