Mysql 使用用户ID数组添加/更新db列的SQL代码
我在WP中创建了一个自定义数据库,在这里我希望根据ID创建用户的追随者和追随者列表Mysql 使用用户ID数组添加/更新db列的SQL代码,mysql,ajax,wordpress,Mysql,Ajax,Wordpress,我在WP中创建了一个自定义数据库,在这里我希望根据ID创建用户的追随者和追随者列表 $table_name = $wpdb->prefix . 'addon_users'; $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, user_id bigint(20) NOT NULL, username b
$table_name = $wpdb->prefix . 'addon_users';
$sql = "CREATE TABLE $table_name (
id mediumint(9) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
username bigint(20) NOT NULL,
following bigint(20) NOT NULL,
followers bigint(20) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY id (id)
) $charset_collate;";
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
我的AJAX调用:
public function addon_ajax_follow_me() {
check_ajax_referer( 'km-ajax-create-nonce', 'security' );
$current_user = get_current_user_id();
$target_user = isset( $_POST['data-follow-user'] ) ? $_POST['data-follow-user'] : false;
if( ! empty( $_POST['data-follow-user'] ) ) {
$this->cgc_follow_user( $current_user, $target_user );
}
wp_die();
}
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
遵循用户功能:
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
此函数用于更新数据库以及我需要帮助的地方
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
public function add_follower( $args = array() ) {
global $wpdb;
$defaults = array(
'user_id' => '',
'following' => ''
);
$args = wp_parse_args( $args, $defaults );
$add = $wpdb->query(
$wpdb->prepare(
"INSERT INTO {$this->table} SET
`user_id` = '%d',
`following` = '%d'
;",
absint( $args['user_id'] ),
absint( $args['following'] )
)
);
if ( $add )
return $wpdb->insert_id;
return false;
}
每当一个用户跟随另一个用户时,就会在数据库中创建一个新条目。但我希望构建一个用户ID数组。大概是这样的:
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
umeta_id user_id username following followers
1 1 Yoonah 5,12,58,66 45, ...
2 5 Elisabeth 2,8,66 1,45, ...
3 8 Max 45,9,99 5, ...
4 45 Ace 1,5,87 8, ...
我偶然发现的是使用适当的SQL代码来添加ID数组
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
另外,当“Yoonah”1出现在伊丽莎白5号之后时,伊丽莎白5号的追随者栏也应该更新为“Yoonah”1
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
非常感谢您的帮助
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
编辑
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
我不太明白你的问题,我希望这就是你所说的
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
更新:
我测试了这段代码,它正在工作,唯一的问题是,如果用户id不存在,我就必须输入一个用户名来创建它,但我认为,因为您使用的是当前登录WordPress的用户,所以它应该可以正常工作,而不是像我一样出现问题
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
Class TestThis {
var $table = 'users';
function __construct() {
add_action( "wp_ajax_add_follower", array( $this, "add_follower" ) );
}
function add_follower() {
$current_user = wp_get_current_user();
$args = array(
'user_id' => $current_user->ID,
'follow_to' => $_POST['user_to_follow']
);
$response = $this->add_following( $args );
if ( ! empty( $response ) && $response !== FALSE ) {
$args = array(
'user_id' => $_POST['user_to_follow'],
'followed_by' => $current_user->ID
);
$this->add_followed_by( $args );
}
wp_die();
}
function add_following ( $args = array() ) {
global $wpdb;
$defaults = array(
'user_id' => "",
'follow_to' => "",
'username' => "Default Username"
);
$args = wp_parse_args( $args, $defaults );
// First you have to check if the user already exists and return his Following Row
$following = array();
$existing = $wpdb->get_var( $wpdb->prepare( "SELECT following FROM {$this->table} WHERE user_id = %d", $args["user_id"] ) );
if( ! empty( $existing ) ) {
$following = json_decode( $existing );
}
// We check if this user ($args["user_id"]) is already following the $args["follow_to"] user.
if( in_array( $args["follow_to"], $following ) )
{
return TRUE;
}else{
array_push( $following, $args['follow_to'] );
}
// We verify if the user exists and update the value, if he does not and we sent username, then, we create it.
if( null === $existing && ! empty( $args['username'] ) ) {
$wpdb->insert(
$this->table,
array(
'user_id' => $args['user_id'],
'username' => $args['username'],
'following' => json_encode( $following ),
'followers' => json_encode( array() )
),
array(
'%d',
'%s',
'%s',
'%s'
)
);
return $wpdb->insert_id;
} else {
$updated = $wpdb->update(
$this->table,
array(
'following' => json_encode( $following )
),
array(
'user_id' => $args["user_id"]
),
"%s",
"%d"
);
return $updated;
}
}
function add_followed_by( $args, $defaults ) {
global $wpdb;
$defaults = array(
'user_id' => "",
'followed_by' => "",
'username' => "Default Username"
);
$args = wp_parse_args( $args, $defaults );
// First you have to check if the user already exists and return his Followers Row
$followers = array();
$existing = $wpdb->get_var( $wpdb->prepare( "SELECT followers FROM {$this->table} WHERE user_id = %d", $args["user_id"] ) );
if( ! empty( $existing ) ) {
$followers = json_decode( $existing );
}
// We check if this user ($args["user_id"]) is already followed by $args["followed_by"] user.
if( in_array( $args["followed_by"], $followers ) )
{
return TRUE;
}else{
array_push( $followers, $args['followed_by'] );
}
// We verify if the user exists and update the value, if he does not and we sent username, then, we create it.
if( null === $existing && ! empty( $args['username'] ) ) {
$wpdb->insert(
$this->table,
array(
'user_id' => $args['user_id'],
'username' => $args['username'],
'following' => json_encode( array() ),
'followers' => json_encode( $followers )
),
array(
'%d',
'%s',
'%s',
'%s'
)
);
return $wpdb->insert_id;
} else {
$updated = $wpdb->update(
$this->table,
array(
'followers' => json_encode( $followers )
),
array(
'user_id' => $args["user_id"]
),
"%s",
"%d"
);
return $updated;
}
}
}
new TestThis();
我使用以下jQuery代码对其进行了测试:
*遵循ID 2的用户应该存在,或者我们应该发送用户名以添加函数后面的
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
jQuery.post(ajaxurl, { 'action' : "add_follower", "user_to_follow" : 2 }, function() {
console.log( "listo" );
}).fail(function(err) { console.log(err); })
如果你有问题,请告诉我
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
更新:取消跟踪的代码
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
if( ( $key = array_search( $args['unfollow_to'], $following ) ) !== false ) {
unset( $following[$key] );
reset( $following );
$updated = $wpdb->update( $this->table,
array(
'following' => json_encode( $following )
),
array(
'user_id' => $args['user_id']
),
'%s', '%d'
);
return $updated;
}
unfollow选项如下所示:
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
更新:
这应该行得通
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}
if( ( $key = array_search( $args['unfollow_to'], $following ) ) !== false ) {
unset( $following[$key] );
reset( $following );
$updated = $wpdb->update( $this->table,
array(
'following' => json_encode( $following )
),
array(
'user_id' => $args['user_id']
),
'%s', '%d'
);
return $updated;
}
这是因为您是按键删除的,而应该按值删除。我忘了告诉您,使用这种方法,我使用的是PHP的json_编码和json_解码,而不是逗号分隔的值。奇怪的是,它不起作用。明天早上我会仔细看一看;为什么更喜欢使用json_编码和json_解码,有什么具体原因吗?我更喜欢在这里使用json_编码和json_解码,因为我们可以在字段中注册一个完整的数组作为字符串,甚至,我们也可以在其中添加一个带逗号的字符串值。它更加健壮和灵活。创建逗号分隔字符串时,必须确保不要在末尾添加逗号,因为它会在数组中创建一个空项,例如23、42、53,明白了!我注意到代码仍然没有按预期工作。用户被完美地添加到数据库中,但followers和后续列的值仍然为0。你可能有什么想法吗?呵呵,我明白了!,很抱歉我忘了告诉你们,followers和following应该是一个文本字段,而不是bigint,因为它是一个字符串,将被存储在它上面。json_编码和json_解码函数所做的是将字符串转换为数组,并将数组转换为字符串。很抱歉再次打扰您,但我注意到数据库中有一些奇怪的东西,每当我单击“取消跟踪”按钮时,就会引起冲突;数组将变成一个对象。将更新我关于它的第一篇帖子:很抱歉,我这周一直很忙,您可以在$wpdb->update中使用json_encode Array$followers和json_encode Array$followers强制对象到数组的转换。谢谢!这确实解决了对象错误。我仍然很好奇为什么在下面的列中会列出引号,而在followers列中却没有;效果稍微好一点
public function cgc_follow_user( $current_user = 0, $user_to_follow = 0 ) {
...
$args = array(
'user_id' => $current_user,
'following' => $user_to_follow
);
$this->add_follower( $args );
}