0

Hi there i'm trying to get data-id from url and use this id to insert into database, to make like dislike system.

data-id="1" it is user id.

The link look like this:

<a data-fav="<?php echo $_SESSION['LANG']['favorite']; ?>" data-fav-active="<?php echo $_SESSION['LANG']['favorited']; ?>" class="favorite favoriteIcon" data="<?php echo $key['id']; ?>" data-id="<?php echo $key['user_id']; ?>" data-token="<?php echo $key['token_id']; ?>">

PHP source

some more information: the link is inside of the message that users write, when someone clicks like message, it goes to function call favorites

public function favorites() {
    /*
     * ----------------------------------------------------------------------------
     *  Add, Remove Favorites
     * @$active  :"Check to see if the user has already added prior to Favourites"
     * @verified : "Check if the publication exists"
     * ----------------------------------------------------------------------------
     */

    $active   = self :: favsUser( null, $_SESSION['authenticated'], $_POST['id'] );
    $verified = self :: checkPost( $_POST['id'], $_POST['token'] ) ? 1 : 0;


    if( $verified == 1 && empty( $active ) )
    {
        $_idPost  = (int)$_POST['id'];
        $_sql     = $this->db->prepare("SELECT user FROM posts WHERE id = :id");
        $_sql->execute( array(  
                            ':id' => $_idPost 
                            ) 
                        );
        $response = $_sql->fetch( PDO::FETCH_OBJ );

        /** If not exists, insert new record  **/
        $sql = $this->db->prepare("INSERT INTO favorites VALUES( null, ?, ?, '1', '".$this->_dateNow."' );");
        $sql->bindValue( 1, $_GET['data-id'], PDO::PARAM_INT );
        $sql->bindValue( 2, $_POST['id'], PDO::PARAM_INT );
        $sql->execute();
        if( $sql->rowCount() !=  0 ) {

            if( $response->user != $_SESSION['authenticated'] ) {
                /* Send Interaction */
            self :: sendInteraction( $response->user, $_SESSION['authenticated'], $_idPost, 3 );    
            }
            return( 1 );
        }

    }

    if( $verified == 1 && !empty( $active ) && $active[0]['status'] == '1' )
    {
        /** If exists, update status to Delete/Trash  **/
        $sql = $this->db->prepare("UPDATE favorites SET status = '0' WHERE id_usr = ? && id_favorite = ? ");
        $sql->bindValue( 1, $_SESSION['authenticated'], PDO::PARAM_INT );
        $sql->bindValue( 2, $_POST['id'], PDO::PARAM_INT );
        $sql->execute();
        if( $sql->rowCount() !=  0 )
        {
            return( 2 );
        }
    }
    else if ( $verified == 1 && !empty( $active ) && $active[0]['status'] == '0' )
        {
            /** If exists and status == Delete/Trash, update status to Active  **/
            $sql = $this->db->prepare("UPDATE favorites SET status = '1' WHERE id_usr = ? && id_favorite = ? ");
            $sql->bindValue( 1, $_SESSION['authenticated'], PDO::PARAM_INT );
            $sql->bindValue( 2, $_POST['id'], PDO::PARAM_INT );
            $sql->execute();
        if( $sql->rowCount() !=  0 )
        {
            return( 3 );
        }
    }
        return false;
        $this->db = null;

   }   

MYSQL starcture

TABLE favorites

COLUMNS: id (automatic), id_usr (user id from data-id that we need to take from url ), id_post (if of post all set), date (date)

Thank you.

4

2 回答 2

0

当您实际单击“锚标记链接”时,您看到的网址是什么?

于 2013-11-14T09:02:02.843 回答
0

您说您想要 URL 参数data-id,但您从未在代码中引用它。只需使用$_GET['data-id'].

编辑:现在我正在更仔细地查看您的问题,我认为您实际上是在问一个关于使用 AJAX 使用 JavaScript 执行此操作的问题。如果是这种情况,我强烈建议您使用 jQuery。然后,您只需使用类似于我在此 jsFiddle中的内容检索 id 。

如果您希望链接实际用作可点击链接,则需要像这样更改它:

<a data-fav="like" data-block-active="like" class="block" data="21" data-id="1" data-token="e6fcbe9adff7764872b8b9a571848084e36cf72a" href="?data-id=1">foo bar text goes here...</a>

然后,您可以实际$_GET['data-id']在您的 PHP 代码中使用。

于 2013-11-14T06:38:59.913 回答