Page 1 of 1

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

Posted: Mon Oct 03, 2011 11:15 am
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.

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

Posted: Mon Oct 03, 2011 11:21 am
by Celauran
orbdrums wrote:I am having trouble using the variable in the html <img src> tag.
You'll need to be more specific.

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

Posted: Mon Oct 03, 2011 11:40 am
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>

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

Posted: Mon Oct 03, 2011 12:08 pm
by Celauran

Code: Select all

<img src="<?php echo $whatever; ?>" />

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

Posted: Mon Oct 03, 2011 12:12 pm
by flying_circus
Celauran wrote:

Code: Select all

<img src="<?php echo htmlspecialchars($whatever, ENT_QUOTES); ?>" />

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

Posted: Mon Oct 03, 2011 12:46 pm
by orbdrums
Thanks guys! That worked with the htmlspecialchars...