0

LastInsertedIDがゼロになり続けます。これは、mysqlストアドプロシージャを使用しているためだと思います。これが私のコードです:

// Set up a new MYSQL PDO Database Connection
$dbConnection = new PDO('mysql:host=localhost;dbname=database;charset=UTF8', 'username', 'password');
//turn off emulation for prepared statement and set error mode option
$dbConnection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

//Create a statement and prepare it with parameters
$stmt = $dbConnection->prepare("CALL AddMovieSet(:UserID, :SelectedLibraryID, :txtName, :txtNotes);");
$stmt->bindValue(':UserID', $_SESSION["UserID"], PDO::PARAM_STR);
$stmt->bindValue(':SelectedLibraryID', $_SESSION["SelectedLibraryID"], PDO::PARAM_STR);
$stmt->bindValue(':txtName', $_GET["txtName"], PDO::PARAM_STR);
$stmt->bindValue(':txtNotes', $_GET["txtNotes"], PDO::PARAM_STR);
//execute the prepared statement
$stmt->execute();

//the stored procedure successfully inserts a row
$Name=$_GET["txtName"];
$Notes=$_GET["txtNotes"];
$InsertedID=$dbConnection->lastInsertId(); 
//$InsertedID is always zero, the table I am inserting a row into has an AUTO_INCREMENT for the first column.
4

1 に答える 1

1

PHP と PDO はlastInsertID()、ストアド プロシージャでバグが発生する場合があります。代わりに、次の MySQL 呼び出しを使用してみてください。

"SELECT LAST_INSERT_ID();"
于 2013-03-22T04:04:30.553 に答える