2

Everytime I check the HTML output of this piece of code, the slashes aren't included. Thus, the background image fails. I even put it through a html to php converter. Im lost; please help.

while($row = mysql_fetch_array($data))
{
    //Echo Theme Template on pages  
    echo "<div style='background-image:url('../uploads/avi/{$row['avi']}')></div>";
    echo "<div class='myname'>{$me}</div>"; 
}
4
  • Do you mean the forward slashes, /? Because nothing you've posted would be in any way responsible for removing forward slashes.
    – user229044
    May 8, 2013 at 18:56
  • First obvious question: Are you sure they saved into the database in the first place? May 8, 2013 at 18:57
  • / doesn't need to be escape afaik, unless it's part of html and not an attribute.
    – Dave Chen
    May 8, 2013 at 18:58
  • @tim-banon Remember to select the best answer to help future users find them easily.
    – Xethron
    May 8, 2013 at 19:56

3 Answers 3

4

the simplest answer would be, you have an unclosed ' on your style attribute..

echo "<div style='background-image:url('../uploads/avi/{$row['avi']}')'></div>";
                                                                                                                                              ^here

but this wouldn't work as is.. so you should adjust the quotes like this:

echo "<div style='background-image:url(\"../uploads/avi/{$row['avi']}\");'></div>";

you can see the broken echo http://codepad.viper-7.com/CGMdUx

and the edited one is here http://codepad.viper-7.com/bEyKFz

i passed it through htmlspecialchars on codepad just so you can see it as string and avoid being rendered as HTML for viewing purposes only..

0
0

You did two things wrong here. You never closed the final quote for the style tag, and using single quotes for both style='' and the url('') cancelled each other out.

I'd recommend always using double quotes for HTML tags.

while($row = mysql_fetch_array($data))
//Echo Theme Template on pages
{
    echo "<div style=\"background-image:url('../uploads/avi/{$row['avi']}');\"></div>";
    echo "<div class='myname'>{$me}</div>";
}

Another thing to consider, always say "View Source" instead of "Inspect" with Firebug or some other tool. The reason you didn't see the URL printed out is because new browsers are "Smart" and try to fix DOM errors. Inspecting the source with Firebug or a similar tool will show you what the browser actually interprets. Viewing the source will show what was actually sent to the browser.

1
  • You were right but the corrected was while($row = mysql_fetch_array($data)) { echo '<div style=\'background-image:url(/uploads/avi/'.$row['avi'].')></div>'; echo '<div class=\'myname\'>'.$me.'</div>'; }
    – Tim Banon
    Jan 8, 2019 at 6:14
0

Because of the inner ' used by the style attribute, you need to use double quotes inside.

echo '<div style="background-image:url(\'../uploads/avi/' . $row['avi'] . "')></div>";

Result

<div style="background-image:url('../uploads/avi/abc.efg')></div>
2
  • 1
    Thanks; but, I solved it rather quickly too. I dropped the closing quotes inside url(). #duh
    – Tim Banon
    May 8, 2013 at 19:07
  • Please post it as an answer for future visitors and improve your question.
    – Kermit
    May 8, 2013 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.