从url php获取用户ID

时间:2013-11-14 06:36:39

标签: php mysql sql json

您好我正在尝试从网址获取 data-id 并使用此ID插入数据库,以制作不喜欢的系统。

data-id =“1”这是用户ID。

链接看起来像这样:

<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来源

更多信息:链接位于用户撰写的消息内,当有人点击喜欢消息时,会转到功能调用收藏夹

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

收藏夹

COLUMNS: ID (自动), id_usr (我们需要从网址获取的数据ID中的用户ID), id_post ( if if post all set),日期(日期)

谢谢。

2 个答案:

答案 0 :(得分:0)

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

编辑:现在我正在更仔细地查看你的问题,我想你实际上是在问一个关于用AJAX做JavaScript的问题。如果是这种情况,我强烈建议您使用jQuery。然后,您只需使用this 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>

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

答案 1 :(得分:0)

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