• <small id='BKwHy'></small><noframes id='BKwHy'>

  • <legend id='BKwHy'><style id='BKwHy'><dir id='BKwHy'><q id='BKwHy'></q></dir></style></legend>
  • <i id='BKwHy'><tr id='BKwHy'><dt id='BKwHy'><q id='BKwHy'><span id='BKwHy'><b id='BKwHy'><form id='BKwHy'><ins id='BKwHy'></ins><ul id='BKwHy'></ul><sub id='BKwHy'></sub></form><legend id='BKwHy'></legend><bdo id='BKwHy'><pre id='BKwHy'><center id='BKwHy'></center></pre></bdo></b><th id='BKwHy'></th></span></q></dt></tr></i><div id='BKwHy'><tfoot id='BKwHy'></tfoot><dl id='BKwHy'><fieldset id='BKwHy'></fieldset></dl></div>

    <tfoot id='BKwHy'></tfoot>
      <bdo id='BKwHy'></bdo><ul id='BKwHy'></ul>

        PHP MySQL 触发器 - 如何传递变量来触发?

        时间:2023-07-15
      1. <legend id='7lMb7'><style id='7lMb7'><dir id='7lMb7'><q id='7lMb7'></q></dir></style></legend>
            <tbody id='7lMb7'></tbody>

          <small id='7lMb7'></small><noframes id='7lMb7'>

          <i id='7lMb7'><tr id='7lMb7'><dt id='7lMb7'><q id='7lMb7'><span id='7lMb7'><b id='7lMb7'><form id='7lMb7'><ins id='7lMb7'></ins><ul id='7lMb7'></ul><sub id='7lMb7'></sub></form><legend id='7lMb7'></legend><bdo id='7lMb7'><pre id='7lMb7'><center id='7lMb7'></center></pre></bdo></b><th id='7lMb7'></th></span></q></dt></tr></i><div id='7lMb7'><tfoot id='7lMb7'></tfoot><dl id='7lMb7'><fieldset id='7lMb7'></fieldset></dl></div>
          • <tfoot id='7lMb7'></tfoot>

              • <bdo id='7lMb7'></bdo><ul id='7lMb7'></ul>
                  本文介绍了PHP MySQL 触发器 - 如何传递变量来触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试查找如何将查询中的变量发送到触发器,但它会影响第二个表.我正在使用它来创建一个日志表,其中更新或插入的任何内容都会记录在日志表中例如

                  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']);
                  

                  触发 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$$
                  

                  这可能吗?

                  推荐答案

                  修复 SQL 注入

                  $username = mysql_real_escape_string($_POST['username']);
                  $password = mysql_real_escape_string($_POST['password']);
                  $sql = "INSERT INTO table1 VALUES ('username','password'); 
                  // You must quote your $vars       ^        ^ ^        ^  like this
                  // or syntax errors will occur and the escaping will not work!. 
                  

                  请注意,将未加密的密码存储在数据库中是一种大罪.
                  请参阅下文了解如何解决该问题.

                  Note that storing unencrypted passwords in a database is a cardinal sin.
                  See below on how to fix that.

                  触发器不允许参数
                  您只能访问刚刚插入表中的值.
                  插入触发器为此有一个虚拟表 new.
                  删除触发器有一个虚拟表 old 来查看要删除的值.
                  更新触发器有 oldnew.

                  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 and new.

                  除此之外,您无法访问任何外部数据.

                  Other than that you cannot access any outside data.

                  DELIMITER $$    
                  
                  //Creates trigger to insert into table1 ( logs ) the userid and patientid ( which has to come from php )    
                  
                  CREATE    
                  TRIGGER ai_table1_each AFTER INSERT ON `baemer_emr`.`table1`    
                  FOR EACH ROW    
                  BEGIN    
                    INSERT INTO table2 VALUES (NEW.idn, NEW.username, NEW.patientid);    
                  END$$    
                  

                  解决方案
                  创建一个黑洞表.
                  黑洞表不存储任何内容,它们存在的唯一原因是用于复制目的,因此您可以为它们附加触发器.

                  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.

                  CREATE TABLE bh_newusers (
                    username varchar(255) not null,
                    password varchar(255) not null,
                    idn integer not null,
                    patient_id integer not null,
                    user_id integer not null) ENGINE = BLACKHOLE;
                  

                  接下来将数据插入黑洞表并使用触发器进行处理.

                  Next insert data into the blackhole table and process that using a trigger.

                  CREATE    
                  TRIGGER ai_bh_newuser_each AFTER INSERT ON `baemer_emr`.bh_newuser
                  FOR EACH ROW    
                  BEGIN    
                    DECLARE newsalt INTEGER;
                    SET newsalt = FLOOR(RAND()*999999);
                    INSERT INTO users (username, salt, passhash) 
                      VALUES (NEW.username, newsalt, SHA2(CONCAT(newsalt, password), 512));
                    INSERT INTO table2 VALUES (NEW.idn, NEW.username, NEW.patient_id);
                  END$$    
                  

                  触发器注意事项
                  您永远不应该将密码以明文形式存储在数据库中.
                  始终使用最安全的散列函数将它们存储为加盐散列 (当前 SHA2 的密钥长度为 512),如触发器中所示.
                  您可以通过执行以下操作来测试某人是否拥有正确的密码:

                  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:

                  SELECT * FROM user 
                  WHERE username = '$username' AND passhash = SHA2(CONCAT(salt,'$password'),512)
                  

                  链接
                  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"的 SQL 注入是如何进行的?XKCD漫画作品?

                  这篇关于PHP MySQL 触发器 - 如何传递变量来触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 google api 时如何设置 refresh_token? 下一篇:为新行轮询 MySQL 表的最快方法是什么?

                  相关文章

                    <bdo id='oHoeE'></bdo><ul id='oHoeE'></ul>
                • <legend id='oHoeE'><style id='oHoeE'><dir id='oHoeE'><q id='oHoeE'></q></dir></style></legend>

                    1. <i id='oHoeE'><tr id='oHoeE'><dt id='oHoeE'><q id='oHoeE'><span id='oHoeE'><b id='oHoeE'><form id='oHoeE'><ins id='oHoeE'></ins><ul id='oHoeE'></ul><sub id='oHoeE'></sub></form><legend id='oHoeE'></legend><bdo id='oHoeE'><pre id='oHoeE'><center id='oHoeE'></center></pre></bdo></b><th id='oHoeE'></th></span></q></dt></tr></i><div id='oHoeE'><tfoot id='oHoeE'></tfoot><dl id='oHoeE'><fieldset id='oHoeE'></fieldset></dl></div>

                      <small id='oHoeE'></small><noframes id='oHoeE'>

                      <tfoot id='oHoeE'></tfoot>