0

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Freddy
  • 683
  • 4
  • 35
  • 114

2 Answers2

0

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

header( "Location: ../profile_page/$favourited_by" );
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
BenM
  • 52,573
  • 26
  • 113
  • 168
  • First of all, thank you for your answer. The `$favourited_by` variable stores the `$username` variable which is the session created for the logged in user. With that header() call, I will always be redirected to the logged in users `profile_page`. – Freddy Mar 07 '16 at 22:31
0

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.

CharlesEF
  • 608
  • 1
  • 15
  • 25