Displaying HTML data from MySQL

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
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Displaying HTML data from MySQL

Post by jescobar »

I'm wanting to create a small time script to learn a little about php, so far I have most of the stuff working but for some reason I can't display the data in html. Here is what I have in the code

Code: Select all

<tr>
  <td class="news"><?php print $news; ?></td>
</tr>
When you enter the news item it is then stored in MySQL as HTML and here is an example of what it displays

Code: Select all

<P>Here is an image</P> 
<P><IMG alt=image hspace=0 src="images/wmp_error.png" border=0></P>
Instead of displaying the actual image, for some reason it's not translating the html and it is displaying the code.

Any ideas? I can provide more info if needed.
rzabin
Forum Newbie
Posts: 4
Joined: Tue Jun 06, 2006 2:54 am

Post by rzabin »

Can you give some more details on how you are deriving the $news variable and what is the actual data in mysql data field?
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

This is a rough draft of a small news script. Here is the code I use to pull information from the database

Code: Select all

<table cellpadding="0" cellspacing="0" class="moduletable">
	<tr>
	  <th valign="top">News</th>
	</tr>
	<tr>
	  <td>
<?php 

include ('config.php');

$sql = "SELECT * FROM JQA_news ORDER by 'id' DESC";
$result = mysql_query($sql);
if ($myrow = mysql_fetch_array($result)) {
do
{
$id=$myrow["id"];
$title=$myrow["title"];
$topicimage=$myrow["topicimage"];
$news=$myrow["news"];
$poster=$myrow["poster"];
$date=$myrow["date"];
 ?>
	  <!-- Begin News Table -->
	  <table width="100%" border="0" cellpadding="0" cellspacing="0" class="tborder">
        <tr>
          <td rowspan="3"><div align="center"><img class="mosimage" src="images/topics/<?php echo "$topicimage"; ?>" /></div></td>
          <td width="81%" height="5" class="newstitle"><?php echo "$title"; ?><hr /></td>
        </tr>
        <tr>
          <td class="tabledescription"><?php print $news; ?></td>
        </tr>
        <tr>
          <td><hr /><span class="postedby">Posted by <?php echo "$poster"; ?> on <?php echo "$date"; ?></span></td>
        </tr>
      </table>
	  
	  <table width="100%" border="0" cellpadding="0" cellspacing="0">
	    <tr>
		  <td>&nbsp;</td>
		</tr>
      </table>
	  <!-- End News Table -->

<?php 
}
while ($myrow = mysql_fetch_array($result));
} 
?>
  
	  </td>
	</tr>
</table>
Here is the database info

Code: Select all

CREATE TABLE `JQA_news` (
  `id` int(20) NOT NULL auto_increment,
  `title` varchar(50) default NULL,
  `topicimage` varchar(100) default NULL,
  `news` mediumtext,
  `poster` varchar(50) default NULL,
  `date` varchar(50) default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `id` (`id`)
) TYPE=MyISAM AUTO_INCREMENT=16 ;
I have compiled most of the information from tutorials and self taught, just beginning of couse :)

The information is submitted over in a regular <textarea> but of couse using html.

Thanks!
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

Does your query return any results in the first place?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Other than the ORDER BY not affecting the query, I don't see anything wrong with the query.

'id' = string
`id` = database, table or column reference.
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

It does not display the HTML, basically if I put the following

Code: Select all

Welcome to our site, this is the news system.
It displays the info with no problem. But if I input any HTML it displays the actual code. For example, if I put in the field the following,

Code: Select all

<P>Here is an image</P> 
<P><IMG alt=image hspace=0 src="images/wmp_error.png" border=0></P>
When you come to the home page, it displays the html code instead of translating it and displaying the image.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Post by andym01480 »

When the news item is entered into the database are you filtering/escaping? htmlentities() for instance????
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


most likely not, here is the code for inserting info to the database

Code: Select all

/* FIX STRING */
		$title = FixString($title);
		$topicimage = FixString($topicimage);
		$news = FixString($news);
		$poster = FixString($poster);
		$date = FixString($date);
		

 $sql = "INSERT INTO JQA_news (
title,
topicimage,
news,
poster,
date
 $addl_insert_crit ) VALUES ( 
'$title',
'$topicimage',
'$news',
'$poster',
'$date'
 $addl_insert_values )";
	if ($sql_debug_mode==1) { echo "<BR>SQL: $sql<BR>"; }
	$result = mysql_query($sql,db() );
	if ($result == 1) {
	echo "<p><DIV align='center'>News Added!</DIV></p>";
	} else {
	echo ThrowError("9994SQL",$sql);
	echo "Error inserting record";
	}

//***** END INSERT SQL *****


/*-------------------------------------------------------------------------------
ADD NEW RECORD (CREATING)
-------------------------------------------------------------------------------*/
	if (!$post) {//THEN WE ARE ENTERING A NEW RECORD
	//SHOW THE ADD RECORD RECORD DATA INPUT FORM

	?>
	<FORM METHOD="POST" ACTION="<?php echo $thispage; ?>?proc=New&post=yes&<?php echo $pagevars; ?>">
	<TABLE ALIGN='center'>
	<TR>
		<TD>
			<B>Title:</B>
		</TD>
		<TD>
	<INPUT type="text" NAME="title" SIZE="20">
	  </TR>
		<TR>
		<TD>
			<B>Topic:</B>
		</TD>
		<TD>
		
	<?php 	
	
	echo "<SELECT NAME=\"topicimage\" SIZE=\"1\">";
	
include ('config.php'); 

//******** BEGIN LISTING THE CONTENTS OF  JQA_news_topics*********
//CONNECTION STRING
mysql_connect($dbserver, $dbuser, $dbpass)
or die ("UNABLE TO CONNECT TO DATABASE");
mysql_select_db($dbname)
or die ("UNABLE TO SELECT DATABASE");

$sql = "SELECT * FROM JQA_news_topics ORDER by 'topic' ASC";
$result = mysql_query($sql);
if ($myrow = mysql_fetch_array($result)) {
do
{
$topic=$myrow["topic"];
$image=$myrow["image"];
	
	echo "<option value=\"$image\">$topic</option>";

}
while ($myrow = mysql_fetch_array($result));
}

	echo "</SELECT>";

	?>

		</TR>
		<TR>
		<TD CLASS='TBL_ROW_HDR'>
			<B>News:</B>
		</TD>
		<TD>
	<textarea  CLASS='TXT_BOX'NAME="news" ROWS="4" cols="35"></TEXTAREA>
		</TR>
		<TR>
		<TD CLASS='TBL_ROW_HDR'>
			<B>Poster:</B>
		</TD>
		<TD>
	<INPUT CLASS='TXT_BOX' type="text" NAME="poster" SIZE="20">
		</TR>
		<TR>
		<TD CLASS='TBL_ROW_HDR'>
			<B>Date:</B>
		</TD>
		<TD>
	<INPUT CLASS='TXT_BOX' type="text" value="<?php echo date('l, F  j, Y') ?> @ <?php echo date('h:i:s A') ?>" NAME="date" SIZE="20" readonly>
		</TR>
		<TR>
	<TD>
	</TD>
	<TD>
		<INPUT CLASS='BUTTON' TYPE="SUBMIT" VALUE="Submit News" NAME="SUBMIT">
		<INPUT CLASS='BUTTON' TYPE="RESET" VALUE="Reset" NAME="RESET">
	</TD>
 </TR>
	</TABLE>
	<BR>
	</FORM>

I have noted as best as I could on here to see the different parts, I also pull topic images from a db table as you can see in there.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

For some reason I have tried several things, nothing seems to work but it makes no sense why would the page not translate the code and display the image, I have used other examples and nothing seems to be working either.

From the input code I displayed above, is there anything wrong with it? I am still not sure where the problem is, wether in the input or the output. Any ideas? Thanks.
User avatar
jescobar
Forum Newbie
Posts: 24
Joined: Mon Jun 06, 2005 4:14 am

Post by jescobar »

I was going to update that I finally figured out the issue, just take off the following line

Code: Select all

$news = FixString($news);
And that allowed the html code to translate properly, thanks for all the comments.
Post Reply