简体   繁体   English

在一个过程中在不同表中插入和更新

[英]Insert and Update in different table in one process

I've created a form containing an input field which has an autocomplete function and the other input field was set as readonly. 我创建了一个包含具有自动完成功能的输入字段的表单,另一个输入字段设置为只读。

<div id="windowContent">
        <form action="rdpw_monitoring_process.php" method="post">
            <table cellpadding="10px">
                <tr>
                    <td>Username</td>
                    <td><input type="text" class="form-control" style="padding-left:5px;" name="auto_username_rdpw" id="auto_username_rdpw" class="auto_username_rdpw" /></td>
                </tr>
                <tr>
                    <td>Default Password</td>
                    <td><input type="text" value="nccc2016" class="form-control" name="default_password_rdpw" id="default_password_rdpw" readonly="readonly" /></td>
                </tr>
            </table>

        <br />
        <div style="padding-right:100px; float:right;"><input type="submit" name="reset" value="RESET" id="reset" class="reset btn btn-info" /></div>
        </form>
    </div>

When I put a username and click the submit button it will automatically update the Table 1 (tbl_userlist) of some fields then it will insert the userid from Table 1 (tbl_userlist) that was base on the updated fields. 当我输入用户名并单击“提交”按钮时,它将自动更新某些字段的表1(tbl_userlist),然后它将基于更新的字段插入表1(tbl_userlist)中的用户ID。 My update query was working but my insert query was not working. 我的更新查询有效,但我的插入查询无效。 Is it possible to combine an insert and update query in different table in one process just like my process code below? 是否可以在一个流程中将插入查询和更新查询合并到不同的表中,就像下面的流程代码一样?

<?php
    include('connection.php');

    $autocomplete_username = $_POST['auto_username_rdpw'];
    $default_password = $_POST['default_password_rdpw'];

    $pdo = new PDO('mysql:host=localhost;dbname=etransmittal', 'root', '');

    date_default_timezone_set('Asia/Hong_Kong');
    $current_date = date('Y-m-d H:i:s');
    $date_expiration_ninety_days = date('Y-m-d H:i:s', strtotime("+90 days"));

    $update_qry = $pdo->prepare("UPDATE tbl_userlist SET user_password = :default_password, lastdate_changepw = :date_change_password, password_expiration = :password_date_expiration WHERE username = :username");

    $insert_qry = $pdo->prepare("INSERT INTO tbl_reset_change_expire_password (userid, lastdate_resetpw)
                                VALUES ((SELECT userid FROM tbl_userlist WHERE lastdate_changepw = :change_password_date LIMIT 1), $current_date)");

    if($update_qry->execute(array(':default_password' => $default_password, ':date_change_password' => $current_date, ':password_date_expiration' => $date_expiration_ninety_days, ':username' => $autocomplete_username))){
        echo "You have successfully edit into default password";
    }
    else{
        echo 'Failed to reset default password.';
    }

    $insert_qry->execute(array(':change_password_date' => $current_date));
?>

No, it's not possible to perform an INSERT to one table and an UPDATE to another table in the same SQL statement. 不,不可能在同一SQL语句中对一个表执行INSERT ,对另一个表执行UPDATE

It might be possible to achieve what you want using a trigger (eg AFTER UPDATE ON the table you are updating, which can perform an INSERT . But you can't pass in an argument to a trigger. The values have to be available from the row you are updating, or from a user-defined variable. 可能可以使用触发器来实现所需的功能(例如, AFTER UPDATE ON的表执行AFTER UPDATE ON ,可以执行INSERT 。但是您不能将参数传递给触发器。必须从您要更新的行或来自用户定义的变量的行。


But that doesn't really seem to be the problem. 但这似乎并不是问题所在。 It seems like what you really want is a transaction, and you only want to perform the INSERT if the UPDATE is successful. 似乎您真正想要的是一个事务,并且您只想在UPDATE成功的情况下执行INSERT

It also seems that you'd want the INSERT to be based on the username of the row that was updated, not the last updated date/time. 似乎您也希望INSERT基于更新的行的username ,而不是上次更新的日期/时间。

In your example code, it seems like you'd want an INSERT statement more like this, assuming username is unique in `tbl_reset_change_expire_password: 在示例代码中,假设username在`tbl_reset_change_expire_password中是唯一的,您似乎更希望这样的INSERT语句:

 INSERT INTO tbl_reset_change_expire_password (userid, lastdate_resetpw)
 SELECT l.userid, :change_password_date 
   FROM tbl_userlist l
  WHERE l.username = :username

If you wanted to do that in a trigger, then you could get the userid value from the row you updated. 如果要在触发器中执行此操作,则可以从更新的行中获取userid值。 But the datetime would need to come from somewhere, like the database server via the NOW() function. 但是日期时间需要来自某个地方,例如通过NOW()函数的数据库服务器。 (The timezone would be dependent on the setting of the MySQL session variable.) (时区取决于MySQL会话变量的设置。)

In the body of an AFTER UPDATE ON tbl_userlist trigger, you could perform an INSERT like this... AFTER UPDATE ON tbl_userlist触发器的主体中,您可以执行这样的INSERT操作:

 INSERT INTO tbl_reset_change_expire_password (userid, lastdate_resetpw)
 SELECT NEW.userid, NOW() 

But that's two separate statements that are executed, but you'd only need to execute the UPDATE from the code. 但这是两个独立的语句,但是您只需要从代码中执行UPDATE The INSERT would be performed "behind the scenes" as it were, via the trigger. 插入操作将通过触发器在“幕后”进行。

Below code may be work properly........ 下面的代码可能正常工作.....

<?php
    include('connection.php');

    $autocomplete_username = $_POST['auto_username_rdpw'];
    $default_password = $_POST['default_password_rdpw'];

    $pdo = new PDO('mysql:host=localhost;dbname=etransmittal', 'root', '');

    date_default_timezone_set('Asia/Hong_Kong');
    $current_date = date('Y-m-d H:i:s');
    $date_expiration_ninety_days = date('Y-m-d H:i:s', strtotime("+90 days"));

    $update_qry = $pdo->prepare("UPDATE tbl_userlist SET user_password = :default_password, lastdate_changepw = :date_change_password, password_expiration = :password_date_expiration WHERE username = :username");

    $insert_qry = $pdo->prepare("INSERT INTO tbl_reset_change_expire_password (userid, lastdate_resetpw)
                                VALUES ((SELECT userid FROM tbl_userlist WHERE lastdate_changepw = :change_password_date LIMIT 1), :change_password_date)");

    if($update_qry->execute(array(':default_password' => $default_password, ':date_change_password' => $current_date, ':password_date_expiration' => $date_expiration_ninety_days, ':username' => $autocomplete_username))){
        echo "You have successfully edit into default password";
    }
    else{
        echo 'Failed to reset default password.';
    }

    $insert_qry->execute(array(':change_password_date' => $current_date));
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM