MYSQL在重复密钥更新时未按预期工作

MYSQL在重复密钥更新时未按预期工作,mysql,Mysql,我的应用程序会在客户登录时自动进行检查,并且只返回一个访问令牌和一个店铺值 下表包含标题和一行示例数据 访问令牌 商店 111111111 商店1 您使用的是纯MySQL吗?我以前做过一个注册和登录页面,作为web应用程序的一部分,并使用Php来实现这一点。我相信您可以将Php用于APK,因此我将用Php编写,但您应该能够轻松地翻译为您的语言 <?php if (isset($_POST['signup-submit'])) { //this is so that the followi

我的应用程序会在客户登录时自动进行检查,并且只返回一个访问令牌和一个店铺值

下表包含标题和一行示例数据

访问令牌 商店 111111111 商店1
您使用的是纯MySQL吗?我以前做过一个注册和登录页面,作为web应用程序的一部分,并使用Php来实现这一点。我相信您可以将Php用于APK,因此我将用Php编写,但您应该能够轻松地翻译为您的语言

<?php
if (isset($_POST['signup-submit'])) {  //this is so that the following can only be done on 
                                         the button press (name of it is signup-submit)
    
    require 'dbh.inc.php';

    $username = $_POST['Username'];
    $email = $_POST['mail'];
    $password = $_POST['pwd'];
    $confirmpassword = $_POST['cpwd']; //this is all off the details of the user passed 
                                         through to be run through this script into the 
                                         database
if (empty($username) || empty($email) || empty($password) || empty($confirmpassword)) {
    header("Location: ../index.php?error=emptyfields&uid=". $username. "&mail=". $email);
    exit();
            }  //checking for empty fields

else if (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9 ]*$/", $username)) {
    header("Location: ../index.php?error=invalidemail&uid");
    exit();
            }
else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    header("Location: ../index.php?error=invalidemailuid");
    exit();
            }
else if (!preg_match("/^[a-zA-Z0-9 ]*$/", $username)) {
    header("Location: ../index.php?error=invaliduid&email=". $email);
    exit();
            }
            else if ($password !== $confirmpassword) {
    header("Location: ../index.php?error=checkpasswords&mail=".$email. "&uid=".$username);
    exit();
            }  //checking all characters used are only that which you allow


else {
        $sql = "SELECT uidusers FROM users WHERE uidusers=?";
        $sqly = "SELECT emailusers FROM users WHERE emailusers=?";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../index.php?error=sqlerror");
            exit();
                    }  //using prepared statements to insert user info
            else {
                mysqli_stmt_bind_param($stmt, "s", $username);
                mysqli_stmt_execute($stmt);
                mysqli_stmt_store_result($stmt);
                $resultCheck = mysqli_stmt_num_rows($stmt);
                if ($resultCheck > 0) {
                header("Location: ../index.php?error=usertaken&mail=". $email);
                exit();
                        }    //checking for existing details
        
                    
                if (!mysqli_stmt_prepare($stmt, $sqlx)) {
                    header("Location: ../index.php?error=sqlerror");
                    exit();
                            }
    
                    
                            if (!mysqli_stmt_prepare($stmt, $sqly)) {
                            header("Location: ../index.php?error=sqlerror");
                            exit();
                                    }
                            else { 
                                    mysqli_stmt_bind_param($stmt, "s", $email);
                                    mysqli_stmt_execute($stmt);
                                    mysqli_stmt_store_result($stmt);
                                    $resultCheck3 = mysqli_stmt_num_rows($stmt);
                                    if ($resultCheck3 > 0) {
                                    header("Location: ../index.php?error=emailtaken");
                                    exit();
                                            }  //storing details
                                            
                        
            
                                    else { 
                                        $sql = "INSERT INTO users (uidusers, emailusers, 
                                                pwdusers, invcode) VALUES (?, ?, ?, ?) ";
                                        $stmt = mysqli_stmt_init($conn);
                                        if (!mysqli_stmt_prepare($stmt, $sql)) {
                                        header("Location: ../index.php?error=sqlerror");
                                        exit();
                                                }

                                                        else {
                                                                $hashedpwd = password_hash($password, PASSWORD_DEFAULT);  //hashing passwords
            
                                                                mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hashedpwd);
                                                                mysqli_stmt_execute($stmt);
                                                                header("Location: ../index.php?signup=success");  //all details stored successfully


至于您的访问令牌,我建议添加一个函数来运行uniqid函数,同时添加另一个函数来检查现有令牌,这样就不会产生重复的令牌。我这样做的原因与您的原因类似,然后使用类似的代码将其写入

我不确定您的店铺ID是什么,但我有两种可能性:

如果它只是一种ID,则在数据库中自动递增它

如果要显示人员输入的店铺,请使用外键将列链接到列出了所有店铺的父表,并将关系设置为级联。然后做一个按钮切换商店,这将

A向数据库发送更新,覆盖用户的子列和子行

B将用户重定向到新商店


我不知道为什么代码的后半部分是绿色的,但是如果你删除我的注释,你应该会很好,尽管我建议你编写自己的代码,这样你就可以看到它是如何工作的,并使它适应你自己的项目

你上面发布的mysql语句可以用在mysql的存储过程中,而不是一般形式,参考-您是否将店铺设置为唯一,只有在复制不应重复的内容时,on dup才有效DOH,good call@riggsfully表中的“店铺”列未设置为唯一。我刚刚让这个专栏变得独一无二,现在,它的更新功能正是我想要的。非常感谢!多年来我一直在努力解决这个问题。谢谢你的回复,这非常有帮助。我正在使用python,但我试图在纯MySQL中使用它。尽管对我来说,将python集成到中要容易得多。