2016-02-04 128 views
-2

我基本上是一个新的程序员,有几年的Oracle经验。我有一个收集家庭和成员信息的表单(它也有一个连接到数据库以检索分支机构列表框的数据库连接的下拉列表)。将lastinsertid赋值给一个变量

在POST上,它将信息保存到数据库中的家庭和成员表。家庭是父表,所以记录必须先创建。然后我需要检索household.id(一个MySql自动增量字段)以保存到成员表中,以将两者关联起来。

执行插入到家庭表时,它工作正常。我打印自动增量字段进行检查。

$dbh->beginTransaction(); 
    $stmt->execute(); 
    print("lastinsertid is really = \n"); 
    // $lastid->$dbh->lastInsertId(); 
    print $dbh->lastInsertId(); 

但我需要在下一个语句中使用它,所以需要存储它。当我这样做(只是改变注释),我会得到错误。

 $dbh->beginTransaction(); 
    $stmt->execute(); 
    print("lastinsertid is really = \n"); 
    $lastid->$dbh->lastInsertId(); 
    // print $dbh->lastInsertId(); 

错误:

PHP Notice: Undefined variable: lastid in C:\Users\Desktop\OECC\form with lastinsertid works.php on line 165
PHP Notice: Trying to get property of non-object in C:\Users\Desktop\OECC\form with lastinsertid works.php on line 165 PHP Fatal error: Call to a member function lastInsertId() on null in C:\Users\Desktop\OECC\form with lastinsertid works.php on line 165

回答

1

$lastid = $dbh->lastInsertId()

您使用->代替=