After inserting a new row into my external database, i want to receive back the new (auto-incremented) ID that was created along with the new row.
<?php
$response = array();
// check for required fields
if (isset($_POST['thread']) && isset($_POST['event_name']) && isset($_POST['fb_id']) && isset($_POST['status'])) {
$event_name = $_POST['event_name'];
$thread = $_POST['thread'];
$fb_id = $_POST['fb_id'];
$status = $_POST['status'];
// include db connect class
require_once __DIR__ . '/db_fb_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO events(event_name, thread) VALUES ('$event_name', '$thread')");
Things are working perfectly fine until here. The row is created inside the events table without further problems. Now i want to get the created ID:
$last_id = $mysqli->insert_id();
This is where the code stops working. I tried some variations of the insert_id statement, but none seemed to work. What is the correct way here? After assigning $last_id with the latest ID, i would want a second query:
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO UserEvents(fb_id, event_id, status) VALUES('$fb_id', '$last_id', '$status')");
Im grateful for any kind of help!
OOP style
insert_id
is a property, not a function:It looks like you're not actually using the object-oriented style of
mysqli
. If you want to be consistent and use procedural style everywhere, you should use: