Кавычки отсутствуют при сохранении данных JSON в базе данных (связанные с AJAX)

Я заметил, что мне не хватает обычных кавычек при использовании данных JSON внутри таблицы базы данных. Это происходит только во втором столбце Followers IDs

введите описание изображения здесьвведите описание изображения здесь

Мой код выглядит следующим образом:

public function 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->follow_user( $current_user, $target_user );
}

wp_die();

}

Следующий шаг…

public function follow_user( $current_user = 0, $user_to_follow = 0  ) {

if ( empty( $user_to_follow ) ) {
return;
}

$args = array(
'user_id'   => $current_user,
'follow_to' => $user_to_follow
);

$response = $this->add_following( $args );

if ( ! empty( $response ) && $response !== FALSE ) {
$args = array(
'user_id'   => $user_to_follow,
'followed_by' => $current_user
);

$this->add_followed_by( $args );

$this->km_follow_me_success( $user_to_follow );

} else {
$this->km_follow_me_error();
}

}

Первый столбец:

    public function add_following ( $args = array() ) {

global $wpdb;

$author_info    = get_userdata( $args["user_id"] );

if ( empty( $author_info->display_name ) ) {
$author_name = $author_info->user_login;
} else {
$author_name = $author_info->display_name;
}$defaults = array(
'user_id'       => '',
'follow_to'     => '',
'username'      => $author_name,
'user_email'    => $author_info->user_email
);

$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, true );
}

// 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'],
'email'     => $args['user_email'],
'following' => json_encode( $following ),
'followers' => json_encode( array() )
),
array( '%d', '%s', '%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;
}

}

Этот код предназначен для обновления второго столбца (и где предполагается, что он где-то пойдет не так).

public function add_followed_by( $args = array()  ) {

global $wpdb;

$author_info    = get_userdata( $args['user_id'] );

if ( empty( $author_info->display_name ) ) {
$author_name = $author_info->user_login;
} else {
$author_name = $author_info->display_name;
}

$defaults = array(
'user_id'       => '',
'followed_by'   => '',
'username'      => $author_name,
'user_email'    => $author_info->user_email
);

$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, true );
}

// 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'],
'email'     => $args['user_email'],
'following' => json_encode( array() ),
'followers' => json_encode( $followers )
),
array( '%d', '%s', '%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;
}}

Любая помощь в правильном направлении очень ценится.

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector