简体   繁体   English

PHP MySQL - INSERT 和 ON DUPLIATE KEY UPDATE 与 WHERE?

[英]PHP MySQL - INSERT and ON DUPLIATE KEY UPDATE with WHERE?

I want to be able for members to add more info (location, story) to their profile and also update their password if needed.我希望会员能够在他们的个人资料中添加更多信息(位置、故事),并在需要时更新他们的密码。

For that I use the following snippet:为此,我使用以下代码段:

$query = mysql_query("
    INSERT INTO 
        members (location, story) 
    VALUES 
        ('$location', '$story') 
    WHERE 
        username='$user' 
    ON DUPLICATE KEY UPDATE 
        hash = '$password', 
        location='$location', 
        story='$story' 
    "); 

This does not work with the "WHERE" part, but if I remove it then the data just gets filled into an empty record, not the user record.这不适用于“WHERE”部分,但如果我删除它,则数据只会填充到空记录中,而不是用户记录中。 How do I properly use the WHERE part in this snippet, so the correct user profile is updated?如何正确使用此代码段中的 WHERE 部分,以便更新正确的用户配置文件?

I have searched up and down the internet and this website, but not found a single solution, which surprises me as this seems to be a very common question?我在互联网和这个网站上下搜索,但没有找到一个解决方案,这让我感到惊讶,因为这似乎是一个非常常见的问题?

Does anyone know how to solve this problem?有谁知道如何解决这个问题?

Thanks in advance!提前致谢!

First, mysql is deprecated, you should use mysqli .首先,不推荐使用mysql ,您应该使用mysqli

Second, make sure you escape your values before entering them in queries.其次,确保在将值输入到查询中之前将它们转义。 mysql_real_escape_string() is a bare minimum. mysql_real_escape_string()是最低要求。

Third, INSERT / ON DUPLICATE KEY UPDATE does not accept WHERE clause.三、 INSERT / ON DUPLICATE KEY UPDATE不接受WHERE子句。 It's use is for avoid duplicating keys.它的用途是避免重复密钥。 For a simple user signup or whatever, you could avoid using IDs / auto increment and use username as primary key.对于简单的用户注册或其他任何事情,您可以避免使用 ID/自动增量并使用username为主键。 But for this you'd need MyISAM or MySQL 5.6+ in order to have fulltext indexing, and in general, this is recommended.但为此,您需要 MyISAM 或 MySQL 5.6+ 才能进行全文索引,一般情况下,建议这样做。

But in this use case, your location and story would be always overwritten.但在这个用例中,您的位置和故事将始终被覆盖。 If this is what you want, you can try whatever I've written in the previous paragraph.如果这是你想要的,你可以尝试我在上一段中写的任何内容。

You can use a INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE construct like您可以使用INSERT INTO ... SELECT FROM ... ON DUPLICATE KEY UPDATE构造,如

INSERT INTO 
    members (location, story) 
SELECT '$location', '$story'
FROM table_name
WHERE username='$user' 
ON DUPLICATE KEY UPDATE 
    hash = '$password', 
    location='$location', 
    story='$story'

Ok, I was finally able to solve this problem on my own.好的,我终于能够自己解决这个问题了。 For anyone who is interested, here is the code:有兴趣的朋友,代码如下:

// if user entered location         
        if (!empty($_POST['location']))
        {
            // if database row empty
            if ($row['location'] == " ")
            {
                mysql_query("INSERT INTO members (location) VALUES ('".$location."') WHERE username='".$user."'");  
            }
            // if database row not empty
            else 
            {
                mysql_query("UPDATE members SET location='".$location."' WHERE username='".$user."'");      
            }
        }
        // if user entered story
        if (!empty($_POST['story']))
        {
            // if database row empty
            if ($row['story'] == " ")
            {
                mysql_query("INSERT INTO members (story) VALUES ('".$story."') WHERE username='".$user."'");    
            }
            // if database row not empty
            else 
            {
                mysql_query("UPDATE members SET story='".$story."' WHERE username='".$user."'");            
            }
        }           

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

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