在mysql触发器中检索php变量的值

在mysql触发器中检索php变量的值,php,mysql,sql,database,Php,Mysql,Sql,Database,我有一个php文件,它插入句子并执行如下触发操作: < html > <body > <?php $s = $_POST['sent']; echo "Entered sentence : $s"; if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches))

我有一个php文件,它插入句子并执行如下触发操作:

    < html > <body > 
    <?php 

    $s = $_POST['sent'];
    echo "Entered sentence : $s";

    if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches)) //Values stored in ma.
    {
        $x = (int) $matches[1][0];  //optionally cast to int
        $y = (int) $matches[1][1];
    }

    $con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: ".mysqli_connect_error();
    }


//    $sql2 = "CREATE TRIGGER MysqlTrigger AFTER INSERT ON table1 FOR EACH ROW BEGIN INSERT INTO temp1(sent,pindex,nindex) VALUES (NEW.sent,".$x.",".$y.");";
    mysqli_query($con,$sql2);

    $sql1 = "INSERT INTO table1 (sent)VALUES('$_POST[sent]')";

    if (!mysqli_query($con, $sql1)) {
        die('Error: '.mysqli_error($con));
    }
    echo "1 record added";
    mysqli_close($con);

    ?>
    </html > </body >
我希望test.php中的$x和$y应该在插入操作时插入到数据库中。当x和y为0,0时,可以在现有代码中插入NEW.sent

如果可能的话,我想在触发器上插入test.php本身的sent、x和y

我用上面的test.php删除了触发器部分的注释,并从mysql数据库中删除了触发器。但是它只在temp没有得到更新时才在表1中插入句子。

-------这段代码很容易被SQL注入(我忽略了它,因为它现在对我来说不是问题)----

如果你知道mysql黑洞表的概念,这是很容易做到的。您创建的it不存储任何值,但可以使用it值执行任何其他任务

所以,插入您想要插入到表中的任何值(变量)插入到创建黑洞表中。 在mysql中,您可以像这样使用它:
NEW.

创建黑洞表:

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;



 My solution for main question:

    php.test:



 < html > <body > 
    <?php 
    if (!empty($_POST['insert'])) {
    echo "Insert"; echo "<br/>";
    $s = $_POST['sent'];
    $flag=0;
    echo "Entered sentence : $s";

    if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches)) //Values stored in ma.
    {
        $x = (int) $matches[1][0];  //optionally cast to int
        $y = (int) $matches[1][1];
    }

    echo "<br/>"; echo $x;
    echo "<br/>"; echo $y; echo "<br/>";

    //---------------DB stuff --------------------

    $con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: ".mysqli_connect_error();
    }

    $sql1 = "INSERT INTO table2 (id,sent,pcount,ncount,flag) VALUES('','$_POST[sent]','".$x."','".$y."','".$flag."')";

    if (!mysqli_query($con, $sql1)) {
        die('Error: '.mysqli_error($con));
    }
    echo "1 record added";
    mysqli_close($con);
    }
    // -------------------------------UPDATE --------------------------

    if (!empty($_POST['update'])) {
    echo "update";echo "<br/>";

    $s = $_POST['sent'];
    $flag=1;
    echo "Entered sentence : $s";

    if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches)) //Values stored in ma.
    {
        $x = (int) $matches[1][0];  //optionally cast to int
        $y = (int) $matches[1][1];
    }

    echo "<br/>"; echo $x;
    echo "<br/>"; echo $y; echo "<br/>";

    //---------------DB stuff --------------------

    $con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: ".mysqli_connect_error();
    }

    $sql1 = "INSERT INTO table2 (id,sent,pcount,ncount,flag)VALUES('$_POST[id]','$_POST[sent]','".$x."','".$y."','".$flag."')";

    if (!mysqli_query($con, $sql1)) {
        die('Error: '.mysqli_error($con));
    }
    echo "1 record added";
    mysqli_close($con);

    }
    ?>
    </html > </body >
CREATE TABLE bh_newusers(
用户名varchar(255)不为空,
密码varchar(255)不为空,
idn整数不为空,
患者id整数不为空,
用户_id整数不为空)引擎=黑洞;
我对主要问题的解答是:
php.test:

----------这段代码易于SQL注入(我忽略了它,因为它现在对我来说不是问题)-----------------------------

如果你知道mysql黑洞表的概念,这是很容易做到的。您创建的it不存储任何值,但可以使用it值执行任何其他任务

所以,插入您想要插入到表中的任何值(变量)插入到创建黑洞表中。 在mysql中,您可以像这样使用它:
NEW.

创建黑洞表:

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;



 My solution for main question:

    php.test:



 < html > <body > 
    <?php 
    if (!empty($_POST['insert'])) {
    echo "Insert"; echo "<br/>";
    $s = $_POST['sent'];
    $flag=0;
    echo "Entered sentence : $s";

    if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches)) //Values stored in ma.
    {
        $x = (int) $matches[1][0];  //optionally cast to int
        $y = (int) $matches[1][1];
    }

    echo "<br/>"; echo $x;
    echo "<br/>"; echo $y; echo "<br/>";

    //---------------DB stuff --------------------

    $con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: ".mysqli_connect_error();
    }

    $sql1 = "INSERT INTO table2 (id,sent,pcount,ncount,flag) VALUES('','$_POST[sent]','".$x."','".$y."','".$flag."')";

    if (!mysqli_query($con, $sql1)) {
        die('Error: '.mysqli_error($con));
    }
    echo "1 record added";
    mysqli_close($con);
    }
    // -------------------------------UPDATE --------------------------

    if (!empty($_POST['update'])) {
    echo "update";echo "<br/>";

    $s = $_POST['sent'];
    $flag=1;
    echo "Entered sentence : $s";

    if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Videos/LinSocket/client '$s'"), $matches)) //Values stored in ma.
    {
        $x = (int) $matches[1][0];  //optionally cast to int
        $y = (int) $matches[1][1];
    }

    echo "<br/>"; echo $x;
    echo "<br/>"; echo $y; echo "<br/>";

    //---------------DB stuff --------------------

    $con = mysqli_connect('127.0.0.1:3306', 'root', 'root', 'test');
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: ".mysqli_connect_error();
    }

    $sql1 = "INSERT INTO table2 (id,sent,pcount,ncount,flag)VALUES('$_POST[id]','$_POST[sent]','".$x."','".$y."','".$flag."')";

    if (!mysqli_query($con, $sql1)) {
        die('Error: '.mysqli_error($con));
    }
    echo "1 record added";
    mysqli_close($con);

    }
    ?>
    </html > </body >
CREATE TABLE bh_newusers(
用户名varchar(255)不为空,
密码varchar(255)不为空,
idn整数不为空,
患者id整数不为空,
用户_id整数不为空)引擎=黑洞;
我对主要问题的解答是:
php.test:


看到这个链接可以更好地帮助你@Johan:你在这里可能会有所帮助@拉杰夫兰詹:
使用
baemer\u emr
$$CREATE DEFINER=
baemer\u emr
@
localhost``你能告诉我什么?拉杰夫兰詹:谢谢你的链接。这真的很有帮助。我不知道黑洞表。如果你喜欢,请查看我的下一个答案!看到这个链接可以更好地帮助你@Johan:你在这里可能会有所帮助@拉杰夫兰詹:
使用
baemer\u emr
$$CREATE DEFINER=
baemer\u emr
@
localhost``你能告诉我什么?拉杰夫兰詹:谢谢你的链接。这真的很有帮助。我不知道黑洞表。如果你喜欢,请查看我的下一个答案!