php code to use a variable in <img src> tag...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

php code to use a variable in <img src> tag...

Post by orbdrums »

I have a variable (pers_img_curr) that I have populated from a field in my database. I can confirm this variable contains the correct data but I am having trouble using the variable in the html <img src> tag. Any suggestions? Thanks all.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php code to use a variable in <img src> tag...

Post by Celauran »

orbdrums wrote:I am having trouble using the variable in the html <img src> tag.
You'll need to be more specific.
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: php code to use a variable in <img src> tag...

Post by orbdrums »

Okay, here is the line I'm attempting to use.

Code: Select all

   <p><img src = \"$pers_img_curr\"></p>
I know it's wrong but I'm not sure where to start fixing it. When I add <?php> before this line it fails.

Here is the suspect code:

Code: Select all

//Select database
$db = mysql_select_db(Users);
  if(!$db) {
	die("Unable to select database");
  }

$curr_mbr = $_SESSION['SESS_LOGIN'];
$query_user = "SELECT * FROM members WHERE `login` = '$curr_mbr'";
$result = mysql_query($query_user);
$row = mysql_fetch_assoc($result);

if (!$result) 
  {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query_user;
    die($message);
  }
	   if(mysql_num_rows($result) == 1) 
           {
echo $row['pers_img'],"<br />";
$pers_img_curr = $row['pers_img'];
echo $pers_img_curr;

  	   }else {
	     //Write failed
	     echo "Write failed";
           }
	
//end new code
?>

<h1 align="center">The Website</h1>
<center><a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a>
  <p>
    Hello, <?php echo $_SESSION['SESS_FIRST_NAME']?>&nbsp;</p>

   <p><img src = \"$pers_img_curr\"></p>

</center>

<h4>
  <p align="center"><a href="/PHP-Login/sql-form.php">Search Contacts</a>
</h4>
</body>
</html>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: php code to use a variable in <img src> tag...

Post by Celauran »

Code: Select all

<img src="<?php echo $whatever; ?>" />
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: php code to use a variable in <img src> tag...

Post by flying_circus »

Celauran wrote:

Code: Select all

<img src="<?php echo htmlspecialchars($whatever, ENT_QUOTES); ?>" />
orbdrums
Forum Commoner
Posts: 82
Joined: Wed Sep 14, 2011 11:42 pm

Re: php code to use a variable in <img src> tag...

Post by orbdrums »

Thanks guys! That worked with the htmlspecialchars...
Post Reply