您好我正在尝试从网址获取 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),日期(日期)
谢谢。
答案 0 :(得分:0)
您说您想要URL参数 data-id ,但您从未在代码中引用它。只需使用$_GET['data-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)
当您实际点击“锚标记链接”时,您看到的网址是什么?