Freddy
Freddy

Reputation: 867

header () call not going where defined

I have an icon which is surrounded by anchor tags in an echo statement:

<a href='/inc/favourite_post.php?id=$thought_id'>
    <span class='glyphicon glyphicon-heart-empty' aria-hidden='true' style='padding-right: 5px;'></span> 
</a>

When this icon is clicked, I need it to perform a PHP query which is found in favourite_post.php.

Here is favourite_post.php :

$getid = $_GET['id'];
$favourited_by = $username;
/***********************/
//query to get user id
$get_uid = mysqli_query ($connect, "SELECT * FROM users WHERE username='$username'");
while($query = mysqli_fetch_array($get_uid)){
    $uid = $query['id'];
}
/***********************/
// get details of the post id and username
$get_id = mysqli_query ($connect, "SELECT * FROM user_thoughts WHERE added_by ='$user'");
$row_query = mysqli_fetch_array($get_id);
$fav_by = $row_query['favourited_by'];
$fav_status = $row_query['fav_status'];

$fav_query = mysqli_query ($connect, "INSERT INTO post_favourites (user_id, thought_id) VALUES ('$uid', '$getid')");

header ("Location: ../profile_page/$added_by");

Problem walk through:

  1. Assume I am logged in as Alice and I am on andersons profile_page. The URL at this point will read http://localhost/profile_page/anderson.
  2. I like a post from Anderson and click the icon, which performs this query to favourite their post.
  3. When a user favourites someone's post, I need the page's location to be on the same address, i.e. stay on http://localhost/profile_page/anderson. So in the header() call, I have specified it's location to be ../profile_page/$added_by. $added_by is the username of the user and should take the user back to http://localhost/profile_page/anderson, but it doesn't.
  4. When the icon is clicked, the address bar reads http://localhost/profile_page/ .. meaning the $added_by variable is not being passed through.

Edit:

Here is where I have defined $added_by:

$get_id = mysqli_query ($connect, "SELECT * FROM user_thoughts WHERE added_by ='$user'");
    $row_query = mysqli_fetch_array($get_id);
        $added_by = $row_query['added_by'];
        $fav_by = $row_query['favourited_by'];
        $fav_status = $row_query['fav_status'];

I have previously used header ("Location: profile_page/$user") on other pages. $user is the var which holds the data after ?u= in the URL. $added_by obtains the same data from the data. $user and $added_by are both usernames for users, which is why I am stumped.

Upvotes: 0

Views: 57

Answers (2)

CharlesEF
CharlesEF

Reputation: 628

This line of code is invalid: <a href='/inc/favourite_post.php?id=$thought_id'>. It should be like this: <a href='/inc/favourite_post.php?id=<?php echo $thought_id;?>'>. Of course, I assumed '$thought_id' is a php variable.

Upvotes: 0

BenM
BenM

Reputation: 53228

I think you actually meant to use the $favourited_by variable in your header() call:

header( "Location: ../profile_page/$favourited_by" );

Upvotes: 0

Related Questions