lastInsertId()返回NULL

时间:2017-12-23 16:54:07

标签: php mysql pdo

我想在我的sql查询中插入最后一个id,但我的varieble返回NULL

这是我的代码

 $conn->beginTransaction();
try{
  $stmt=$conn->prepare("SELECT cn_id FROM food_category WHERE catname= :catname");
  $stmt->bindParam(':catname', $_POST['catname'], PDO::PARAM_STR);
  $stmt->execute();
  $catres= $stmt->fetch(PDO::FETCH_ASSOC);
  if(count($catres) > 0 ){
    $cn_id= $catres['cn_id'];
  }else{
    $stmt1= $conn->prepare("INSERT INTO food_category (catname) VALUES (:catname)");
    $stmt1->bindParam(':catname', $_POST['catname'], PDO::PARAM_STR);
    $stmt1->execute();
    $cn_id= $stmt1->lastInsertId();
  }

  $stmt2=$conn->prepare("SELECT cn_id FROM food_category_en WHERE catname= :catname_en");
  $stmt2->bindParam(':catname_en', $_POST['catname_en'], PDO::PARAM_STR);
  $stmt2->execute();
  $catresen= $stmt2->fetch(PDO::FETCH_ASSOC);
  if(count($catresen) > 0 ){
    $cn_id_en= $catresen['cn_id'];
  }else{
    $stmt3= $conn->prepare("INSERT INTO food_category_en (catname) VALUES (:catname_en)");
    $stmt3->bindParam(':catname_en', $_POST['catname_en'], PDO::PARAM_STR);
    $stmt3->execute();
    $cn_id_en= $stmt3->lastInsertId();
  }

  $stmt4=$conn->prepare("INSERT INTO food_category_main (st_id, cn_id, cn_id_en, catcount) VALUES (:st_id, :cn_id, :cn_id_en, :catcount)");
  $stmt4->bindParam(':st_id', $_POST['st_id'], PDO::PARAM_INT);
  $stmt4->bindParam(':cn_id', $cn_id, PDO::PARAM_INT);
  $stmt4->bindParam(':cn_id_en', $cn_id_en, PDO::PARAM_INT);
  $stmt4->bindParam(':catcount', $noc, PDO::PARAM_INT);
  $stmt4->execute();

$stmt5=$conn->prepare("UPDATE magazia_main SET noc= :noc WHERE st_id= :st_id");
$stmt5->bindParam(':st_id', $_SESSION['st_id'], PDO::PARAM_INT);
$stmt5->bindParam(':noc', $noc, PDO::PARAM_INT);
$stmt5->execute();

$conn->commit();
header ("Location:dashboard.php?store=".$_SESSION['name']);
exit;
   }catch(Exception $e){
//An exception has occured, which means that one of our database queries
//failed.
//Print out the error message.
echo $e->getMessage();
//Rollback the transaction.
$conn->rollBack();

 }

我在php手册here中读到,如果我使用事务,则lastInsertId将返回0.如您所见,lastInsertId(stmt1中的第1个,stmt3中的第2个)位于$ conn-> commit()之前;,虽然它们返回NULL。

food_category中的

cn_id和food_category_en中的cn_id都是主键和AUTO_INCREMENT

我在互联网上搜索一些解决方案,但似乎没有一个能解决我的问题。

这是来自php我的管理员的打印屏幕 enter image description here

***连接数据库****      

 $servername = "localhost";
 $username = "root";
 $password = "12345";
 try {
  $conn = new PDO("mysql:host=$servername;dbname=superdb;     charset=utf8", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  }
  catch(PDOException $e)
  {
  echo "Connection failed: " . $e->getMessage();
  }
  ?>

有什么想法吗?

提前致谢

Vaggelis

2 个答案:

答案 0 :(得分:0)

经过几个小时的搜索,我没有找到lastInsertId()返回NULL的原因。无论如何改变了我的代码,现在正在工作。

我从事务中删除了SELECT查询,并将它们放在if语句中,该语句检查输入是否有值。之后我在pdo事务中创建了两个if语句来检查值cn_id = 0或cn_id_en = 0。

现在是我的代码:

ACK

我不会接受这几天作为答案,以便从尽可能多的用户可以看出

答案 1 :(得分:-1)

请将$stmt1$stmt3替换为$conn您的代码是这样的

$conn->beginTransaction();
try{
  $stmt=$conn->prepare("SELECT cn_id FROM food_category WHERE catname= :catname");
  $stmt->bindParam(':catname', $_POST['catname'], PDO::PARAM_STR);
  $stmt->execute();
  $catres= $stmt->fetch(PDO::FETCH_ASSOC);
  if(count($catres) > 0 ){
    $cn_id= $catres['cn_id'];
  }else{
    $stmt1= $conn->prepare("INSERT INTO food_category (catname) VALUES (:catname)");
    $stmt1->bindParam(':catname', $_POST['catname'], PDO::PARAM_STR);
    $stmt1->execute();
    $cn_id= $conn->lastInsertId();
  }

  $stmt2=$conn->prepare("SELECT cn_id FROM food_category_en WHERE catname= :catname_en");
  $stmt2->bindParam(':catname_en', $_POST['catname_en'], PDO::PARAM_STR);
  $stmt2->execute();
  $catresen= $stmt2->fetch(PDO::FETCH_ASSOC);
  if(count($catresen) > 0 ){
    $cn_id_en= $catresen['cn_id'];
  }else{
    $stmt3= $conn->prepare("INSERT INTO food_category_en (catname) VALUES (:catname_en)");
    $stmt3->bindParam(':catname_en', $_POST['catname_en'], PDO::PARAM_STR);
    $stmt3->execute();
    $cn_id_en= $conn->lastInsertId();
  }

  $stmt4=$conn->prepare("INSERT INTO food_category_main (st_id, cn_id, cn_id_en, catcount) VALUES (:st_id, :cn_id, :cn_id_en, :catcount)");
  $stmt4->bindParam(':st_id', $_POST['st_id'], PDO::PARAM_INT);
  $stmt4->bindParam(':cn_id', $cn_id, PDO::PARAM_INT);
  $stmt4->bindParam(':cn_id_en', $cn_id_en, PDO::PARAM_INT);
  $stmt4->bindParam(':catcount', $noc, PDO::PARAM_INT);
  $stmt4->execute();

$stmt5=$conn->prepare("UPDATE magazia_main SET noc= :noc WHERE st_id= :st_id");
$stmt5->bindParam(':st_id', $_SESSION['st_id'], PDO::PARAM_INT);
$stmt5->bindParam(':noc', $noc, PDO::PARAM_INT);
$stmt5->execute();

$conn->commit();
header ("Location:dashboard.php?store=".$_SESSION['name']);
exit;
   }catch(Exception $e){
//An exception has occured, which means that one of our database queries
//failed.
//Print out the error message.
echo $e->getMessage();
//Rollback the transaction.
$conn->rollBack();

 }

如果您通过SQL查询获得

$stmt1 = $db->query("SELECT LAST_INSERT_ID()");
$lastId = $stmt1->fetchColumn();