我想从wordpress中的wp_usermeta表更新一些元键。我已经使用了这段代码但找不到我的解决方案。我已经尝试过这段代码了。任何帮助都将受到高度赞赏
$newname=$_POST['name'];
$parts = explode(" ", $newname);
$newlname = array_pop($parts);
$newfname = implode(" ", $parts);
echo "Firstname: $newfname\n";
echo "Lastname: $newlname\n";
echo $id= $_POST['newid'];
echo $newaddress= $_POST['address'];
echo $newcity=$_POST['city'];
echo $newareacode=$_POST['acode'];
/* Start database connection */
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db('etplpm_network');
/* End database connection */
$sql="UPDATE wp_usermeta set 'first_name'=$newfname, 'last_name'=$newlname, 'billing_address_1'=$newaddress, 'billing_city'=$newcity, 'billing_postcode'=$newareacode WHERE 'ID'=$id";
$result = mysql_query( $sql, $conn );
if($result)
{
echo "Successfully Updated";
}
else
{
echo "Successfully Not Updated";
}
}
答案 0 :(得分:1)
您的MySql语法错误:
UPDATE wp_usermeta set 'first_name'=$newfname, 'last_name'=$newlname, 'billing_address_1'=$newaddress, 'billing_city'=$newcity, 'billing_postcode'=$newareacode WHERE 'ID'=$id;
您必须用引号字段值换行,而不是字段名:
UPDATE wp_usermeta SET first_name = '$newfname', ...
但是这个查询也会失败,因为你还没有检查wp_usermeta
table:
CREATE TABLE `wp_usermeta` (
`umeta_id` bigint(20) UNSIGNED NOT NULL,
`user_id` bigint(20) UNSIGNED NOT NULL DEFAULT '0',
`meta_key` varchar(255) DEFAULT NULL,
`meta_value` longtext
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
因此,您首先要检查特定用户是否存在元数据:
SELECT umeta_id FROM wp_usermeta WHERE user_id=$id AND meta_key='first_name'
然后,如果元存在,请更新它:
UPDATE wp_usermeta SET meta_value='$newfname' WHERE umeta_id=...
否则,插入新的:
INSERT INTO wp_usermeta (user_id, meta_key, meta_value) VALUES ('$id', 'first_name', '$newfname')
但是,为什么不使用worpress函数update_user_meta()
?
update_user_meta( $id, 'first_name', '$newfname' );
update_user_meta( $id, 'last_name', '$newlname' );
update_user_meta( $id, 'billing_address_1', '$newaddress' );
update_user_meta( $id, 'billing_city', '$newcity' );
update_user_meta( $id, 'billing_postcode', '$newareacode' );