mysql insert 返回_mysql_insert_id()返回0

bd96500e110b49cbb3cd949968f18be7.png

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?

here's my code:

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

{

$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("\r\n","\n",$insert))."\n\n".str_replace(" ","",str_replace("\r\n","\n",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)

解决方案

According to the manual mysql_insert_id returns:

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.

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.

There are two possibilities I can think of:

Your table doesn't have an auto_increment field

Since you doesn't provide the link to the mysql_insert_id() but using a link with mysql_query() it might not be the correct table that's queried when retrieving the last inserted id.

Solution:

Make sure it has an auto_increment field

Provide the link aswell: $waarde = mysql_insert_id($this->db);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值