PHP MySQL 触发器 - 如何将变量传递给触发器?
我试图找到如何将查询中的变量发送到触发器,但这会影响第二个表。我使用它来创建一个日志表,其中更新或插入的任何内容都会记录在日志表中例如
//Inserts into the table the username and password
$sql = "INSERT INTO table VALUES ($_POST['username'], $_SESSION['password']);
触发 DDL 语句
DELIMITER $$
//Creates trigger to insert into table1 ( logs ) the userid and patientid ( which has to come from php )
USE `baemer_emr`$$
CREATE
DEFINER=`baemer_emr`@`localhost`
TRIGGER `table1`.`after_insert`
AFTER INSERT ON `baemer_emr`.`table1`
FOR EACH ROW
BEGIN
INSERT INTO table2 VALUES (NEW.idn, $_POST[userid], $_SESSION[patientid]);
END$$
这可能吗?
I'm trying to find how to send variables in a query to the trigger, but it's going to affect a second table. I'm using this to create a log table, in which anything that gets updated or inserted gets recorded in the log table For example
//Inserts into the table the username and password
$sql = "INSERT INTO table VALUES ($_POST['username'], $_SESSION['password']);
Trigger DDL Statements
DELIMITER $
//Creates trigger to insert into table1 ( logs ) the userid and patientid ( which has to come from php )
USE `baemer_emr`$
CREATE
DEFINER=`baemer_emr`@`localhost`
TRIGGER `table1`.`after_insert`
AFTER INSERT ON `baemer_emr`.`table1`
FOR EACH ROW
BEGIN
INSERT INTO table2 VALUES (NEW.idn, $_POST[userid], $_SESSION[patientid]);
END$
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
修复 SQL 注入
请注意,在数据库中存储未加密的密码是一项重大罪过。
请参阅下文了解如何解决该问题。
触发器不允许参数
您只能访问刚刚插入表中的值。
插入触发器有一个用于此目的的虚拟表
new
。删除触发器有一个虚拟表
old
来查看要删除的值。更新触发器同时具有
old
和new
。除此之外,您无法访问任何外部数据。
解决方案
创建黑洞表。
黑洞表不存储任何内容,它们存在的唯一原因是为了复制目的,因此您可以将触发器附加到它们。
接下来将数据插入黑洞表并使用触发器进行处理。
触发器注释
您永远不应该将密码以明文形式存储在数据库中。
始终使用最安全的哈希函数(当前为密钥长度为 512 的 SHA2)将它们存储为加盐哈希,如触发器中所示。
您可以通过执行以下操作来测试某人是否拥有正确的密码:
链接
http://dev.mysql.com/doc/ refman/5.0/en/blackhole-storage-engine.html
http://dev.mysql.com/doc/refman/ 5.0/en/create-trigger.html
在 MySQL 中存储哈希密码
“Bobby Tables”XKCD 漫画中的 SQL 注入是如何工作的?
Fix that SQL-injection
Note that storing unencrypted passwords in a database is a cardinal sin.
See below on how to fix that.
Triggers do not allow parameters
You can only access the values you just inserted into the table.
The Insert trigger has a dummy table
new
for this.The Delete triger has a dummy table
old
to see the values that are to be deleted.The Update trigger has both
old
andnew
.Other than that you cannot access any outside data.
The solution
Create a blackhole table.
Blackhole tables to not store anything, their only reason to exist is for replication purposes and so you can attach triggers to them.
Next insert data into the blackhole table and process that using a trigger.
Notes on the trigger
You should never store passwords in the clear in a database.
Always store them as a salted hash using the safest hash function (currently SHA2 with a 512 key length) , as shown in the trigger.
You can test to see if someone has the correct password by doing:
Links
http://dev.mysql.com/doc/refman/5.0/en/blackhole-storage-engine.html
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Storing hashed passwords in MySQL
How does the SQL injection from the "Bobby Tables" XKCD comic work?
我不认为是这样。
您可以将
userid
和patentid
列添加到VALUES
表中,在原始插入期间填充它们,然后在插入到您的表中时在触发器中引用这些列。日志表。然后是触发器:
I don't think it is.
You could add
userid
andpatientid
columns to theVALUES
table, populate them during the original insert and then reference those within the trigger when inserting into your log table.and then the the trigger: