MySQL事务 - 更新2个表

时间:2015-12-11 00:29:57

标签: php mysql sql

我有一个表单要求用户验证他们的电子邮件地址,该链接将作为

发送给用户

http://app.myurl.org/h/activate.php?email=useremail%40gmail.com&key=80fddb7fa21dd2e2639ae5ec82b9d511&api=8a2d01d7411ec2488307744ddf070a4d

将用户定向到activate.php

我正在尝试从网址获取emailkeyapi

然后我尝试更新用户表和名册表

要更新的

wrightway_roster表格列为Activation

要更新的

wrightway_users表格列为groups

它们都传递MD5随机散列的唯一值。一切都顺利进行,而不是TRANSACTION查询。

<?php
include ('database_connection.php');
if (isset($_GET['email']) && preg_match('/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/', $_GET['email'])) { $email = $_GET['email']; }
if (isset($_GET['key']) && (strlen($_GET['key']) == 32)) { $key = $_GET['key'];}
if (isset($_GET['api']) && (strlen($_GET['api']) == 32)) { $API = $_GET['api'];}
if (isset($email) && isset($key)) {
    mysqli_query($dbc, "START TRANSACTION");
    $result_activate_account = mysqli_multi_query($dbc,"
       UPDATE table_users SET groups=[99] WHERE(pinAPP_API ='$API') LIMIT 1;
       UPDATE wrightway_roster SET Activation=NULL WHERE(email ='$email' AND Activation='$key')LIMIT 1;
    ");
    if ($result_activate_account !== false) {
        mysqli_query($dbc, "COMMIT");
        echo '<div>You may now proceed.</div>';
    } else {
        mysqli_query($dbc, "ROLLBACK");
        echo '<div>Oops !You could not be validated. Please recheck the link or contact your hiring manager.</div>';
    }
    mysqli_close($dbc);
} else {
    echo '<div>An Error Occurred.</div>';
}
?>

PHP是5.4 Native

Activationgroups VARCHAR

这些查询可以正常运行,并且可以完成预期的工作。

"UPDATE wrightway_users SET groups='[99]' WHERE(pinAPP_API ='$API') LIMIT 1;";

UPDATE wrightway_roster SET Activation=NULL WHERE(email ='$email' AND Activation='$key')LIMIT 1";

我正在使用单个查询或者我需要它做什么,我写了上面的内容来使用事务。

if (isset($email) && isset($key)) {

     // Update the database to set the "activation" field to null

     $query_activate_account = "UPDATE wrightway_users SET groups='[99]' WHERE(pinAPP_API ='$API') LIMIT 1;";

     $result_activate_account = mysqli_query($dbc, $query_activate_account);

     // Print a customized message:
     if (mysqli_affected_rows($dbc) == 1) //if update query was successful
     {
     echo '<div>You may now proceed.</div>';

     } else {
     echo '<div>Oops !You could not be validated. Please recheck the link or contact your hiring manager.</div>';

     }

     mysqli_close($dbc);

    } else {
     echo '<div>An Error Occurred.</div>';
    }

4 个答案:

答案 0 :(得分:2)

MySQL不接受此语法

UPDATE wrightway_users SET groups=[99] ...
                                  ^  ^

也许其中一个会更好用:

UPDATE wrightway_users SET groups=99 ...
UPDATE wrightway_users SET groups="99"
UPDATE wrightway_users SET groups="[99]"

根据您的需要和群组的类型

阅读this answer似乎证实了这一点。

答案 1 :(得分:1)

能够让它运转起来。这就是我使用的,并且完全按照需要工作!

if (isset($email) && isset($key)) {
    mysqli_query($dbc);
    $verified = mysqli_query($dbc,"
       UPDATE tbl_users t1, tbl_roster t2 SET t1.groups='[22]', t2.prescreen='0' WHERE t1.API='$API' AND t2.API='$API' AND t2.prescreen='$key';
    ");
    if ($verified !== false) {
        mysqli_query($dbc);
        echo '<br><center><div class="success"></div>';

    } 
    else {
        mysqli_query($dbc);
        echo '<br><div class="validation"></div>';
    }
    mysqli_close($dbc);
  } else {
    echo '<br><div class="error">An Error Occurred.</div>';
}

答案 2 :(得分:0)

您是否尝试过mysqli_begin_transaction(),mysqli_rollback()和mysqli_commit()函数而不是在mysqli_query()中调用begin transaction?

文档:

https://secure.php.net/manual/en/mysqli.begin-transaction.php

答案 3 :(得分:0)

你确定你的错误不在其他地方吗?就像99周围的括号一样?

我收到此错误:

#1064 - You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near '[33] where id=1' at line 1

您可以使用mysqli_error检查上一个错误。