php - 更新 MySQL 中的 users_meta 中的表(不是 WordPress)

标签 php mysql

我想对我保存“用户元信息”的表执行更新查询。

我要更新“userid”=9对应的meta_value,其中可以有多个meta_key进行更新。 (这不是wordpress数据库)

我的表格如下所示:

umeta_id     userid      meta_key       meta_value
 1              9         mobile        123324
 2              9         address       some address
 3              9         city          some city
 4              9         country       some country
 5              9         occupation    some details
 6              9         website       someurl
 7              9         mobile        123324
 8              9         address       some address
 9              9         city          some city
 10             10        country       some country
 11             10        occupation    some details
 12             10        website       someurl

请为此提出一些解决方案。

最佳答案

更新 1:

If i want to update multiple values, which can be any meta_key which is not right now, but can be there. i am using PHP to update this my function looks like this

public static function update
  ( $fullname, $mobile, $interest, $occupation,
    $address, $city, $country, $about, $‌​website ) {
  $sql = "update user_meta SET ";
};

您可以使用 CASE WHENIF( ... ) 条件来更新列中的多行。

下面的例子应该对你有帮助。

使用IF(...)函数:

update user_meta_info
  set
     meta_value = if( meta_key='mobile', '$mobile', meta_value ),
     meta_value = if( meta_key='address', '$address', meta_value ),
     meta_value = if( meta_key='city', '$city', meta_value ),
     meta_value = if( meta_key='country', '$country', meta_value ),
     meta_value = if( meta_key='occupation', '$occupation', meta_value ),
     meta_value = if( meta_key='website', '$website', meta_value )
where
  user_id = 9

使用CASE ... WHEN ...:

update user_meta_info
  set
     meta_value = case meta_key
                        when 'mobile' then '$mobile'
                        when 'address' then '$address'
                        when 'city' then '$city'
                        when 'country' then '$country'
                        when 'occupation' then '$occupation'
                        when 'website' then '$website'
                        else meta_value
                  end
where
  user_id = 9
<小时/>

您必须在 where 子句中使用 meta_keymeta_value 列设置新值。

update user_meta_info
 set meta_value='new mobile number'
where
 user_id = 9 and
 meta_key = 'mobile'

如果每个 meta_key 可以有多个元值,则必须在 where 子句中使用 umeta_id。如果 umeta_id 是主键,则可以省略在 where 子句中使用其他字段。

update user_meta_info
 set meta_value='new mobile number'
where
 umeta_id = 7 -- ( and user_id = 9 and meta_key = 'mobile' )

关于php - 更新 MySQL 中的 users_meta 中的表(不是 WordPress),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21156514/

相关文章:

php - 拉拉维尔 5.3 : Undefined property: Illuminate\Database\Eloquent\Collection Error

c++ - 仅选择 MySQL 数据库/表中的更新/新行

php - Laravel artisan 路线 :call Throws MySQL Error

MySQL:子查询的麻烦

mysql - 创建嵌套存储过程

Mysql - 创建触发器以根据插入到另一个表中的行插入新行

php - 这个 Switch 语句如何知道要执行哪种情况? (PHP/MySQL)

php - SVG/PNG扩展开关

php - 我如何使用 Jquery EasyUI 将我的文件上传到 DB mysql?

php - 函数中的 Mysql 函数我缺少什么