<?php// 1) Insert the request
$stmt = $LINK->prepare("
INSERT INTO TicketRequest
(fi_workflow, fi_state, iam_inserted, fi_user)
VALUES
(
?,
(SELECT id_state FROM TicketStates
WHERE fi_workflow=?
ORDER BY no LIMIT 1
),
?,
?
)
");
$stmt->bind_param('iisi', $wf, $wf, $userName, $userId);
$stmt->execute();
$reqId = $stmt->insert_id;
$stmt->close();
// 2) Record the initial state in TicketHasState
// We re-use the same sub-select to get the id_state again,
// or you can cache it in PHP from the INSERT above.
$initStateSql = "
SELECT id_state
FROM TicketStates
WHERE fi_workflow=?
ORDER BY no
LIMIT 1
";
$st = $LINK->prepare($initStateSql);
$st->bind_param('i', $wf);
$st->execute();
$st->bind_result($initialState);
$st->fetch();
$st->close();
$hist = $LINK->prepare("
INSERT INTO TicketHasState
(fi_request, fi_state, iam_inserted)
VALUES
(?, ?, ?)
");
$hist->bind_param('iis', $reqId, $initialState, $userName);
$hist->execute();
$hist->close();
?>