這是我的代碼,我的 $sql 變數沒有給出查詢,請幫我解決這個問題,我試試這個,但我不能幫我解決這個問題
<?php
$connect = mysqli_connect("localhost", "root", "", "finger");
$f= "";
$l= "";
$sql = "CREATE TRIGGER `ersdmmmmecv` AFTER INSERT ON `event` FOR EACH ROW SELECT fname,Lname INTO $f,$l FROM user WHERE id=NEW.id;"
$result = mysqli_query($connect, $sql);
?>
uj5u.com熱心網友回復:
您不能使用 MySQL 觸發器來更新 PHP 變數。如果您希望在新記錄插入表中時更新$f和的值,$l則event需要完全在 PHP 中執行此操作。
沿著這些路線的東西應該可以作業(注意:我自己沒有測驗過):
$f = "";
$l = "";
$new_id = "id_value";
$insert = $connect->prepare("INSERT INTO `event` (`id`, `column2`, `column3`) VALUES (?, ?, ?)");
$insert->bind_param("sss", $new_id, "value2", "value3");
if ($insert->execute() === FALSE) {
echo 'Could not insert event: ' . $insert->error;
} else {
// If `event`.`id` is actually an AUTO_INCREMENT column, and you don't
// specify it in your INSERT query, use this here:
// $new_id = $insert->insert_id;
$select = $connect->prepare("SELECT `fname`, `Lname` FROM `user` WHERE `id` = ?");
$select->bind_param("s", $new_id);
$select->execute();
$select->bind_result($f, $l);
$success = $select->fetch();
if ($success !== TRUE) {
echo 'Could not update $f and $l with new values: '
. ($select->error ?: 'No user with id: ' . $new_id);
}
}
如果您的代碼中有多個地方可以將資料插入events表中,我會親自將其包裝在一個函式中,這樣我就不必每次都重復此操作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/391748.html
