skip to Main Content
<a href="https://123.com/'.$_SESSION["usersId"].' " target="_blank"><img src="./images/w.jpg"></a>

Hello, I’m new to coding and I was wondering how can I make a custom link with an user ID directly taken from my database. The name usersId is a column in a mySQL table and I take that usersId from a session. Can you please help me fix the problem

I tried to use a $_SESSION to insert the user id in the link but it seem that it didn’t work and when I open a session on my website it just removes the $_SESSION and give me a link without the user id at the end.

2

Answers


  1. Have you defined your session object?
    It should look like this;

    $_SESSION["userId"] = your_mysql_object["userId"]
    
    Login or Signup to reply.
  2. You need to break out of the PHP mode in order to echo the session variable:

    <a href="https://123.com/<?= htmlentities($_SESSION['usersId']) ?>" target="_blank">
        <img src="./images/w.jpg">
    </a>
    

    Note that I also used htmlentities() to encode the ID as an HTML attribute value, to avoid XSS vulnerabilities.
    I strongly recommend you do not learn HTML by writing it as strings in PHP – learn proper HTML first, then inject dynamic values with PHP where needed. Mixing the two like you did just leads to a mess.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search