我在某些条件下有插入前触发器,如果条件:false
发生错误。我应该将数据库触发器中发生的错误消息显示到HTML
网页以通知用户!
CREATE TRIGGER check_trigger BEFORE INSERT ON your_table
FOR EACH ROW
BEGIN
IF (new.ref_owner = new.partnerCodeOwner1)
THEN
SIGNAL SQLSTATE '45000'
SET MESSAGE_TEXT = 'You can not use your ID. Insertion canceled';
END IF;
END
这里插入代码!我使用mysql_sqlstate() returns the SQLSTATE value
但没有捕获错误消息
$user_check_query = "SELECT partnerCodeOwner1
FROM refer
where randomfield='$partnerCode'";
$result = mysqli_query($db, $user_check_query);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if(empty($row['partnerCodeOwner1'])) {
$query = "update refer
set partnerCodeOwner1= '" . $_SESSION['username']
. "' where randomfield='$partnerCode'";
printf("Error - SQLSTATE %s.\n", mysqli_sqlstate($query));
}else {
$query = " update refer
set partnerCode= '" . $_SESSION['username'] . "'
where randomfield='$partnerCode' ";
//printf("Error message: %s\n", $mysqli->error);
}
mysqli_query($db, $query);
我应该从触发器收到错误消息以防万一false
!