PHP in HTML
Posted: Thu Jun 17, 2010 9:38 pm
When I'm trying to use this PHP code in HTML the output looks like this:

>>>>>> CODE: <<<<<
How do I fix this? my PHP code works by itself:


>>>>>> CODE: <<<<<
Code: Select all
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>Buy Games!</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<link href="styles.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div id="content">
<div id="menu">
<ul>
<li id="button1"><a href="home.html" title="">Home</a></li>
<li id="button2"><a href="" title="">Buy</a></li>
<li id="button3"><a href="sell.html" title="">Sell</a></li>
<li id="button4"><a href="#" title="">News</a></li>
<li id="button5"><a href="#" title="">Forum</a></li>
</ul>
</div>
<div id="main">
<div id="right">
<div id="header">
<div id="logo">
<h1> FOR ALL YOUR GAMING NEEDS!</h1>
</div>
</div>
<h4>Sell Games</h4>
<?php // sqltest.php
// Note: This example is different to the one in the book. It has
// been amended to work correctly when deleting entries.
$db_hostname = '127.0.0.1';
$db_database = 'assign1';
$db_username = 'root';
require_once 'sell.php';
$db_server = mysql_connect($db_hostname, $db_username);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($db_database, $db_server)
or die("Unable to select database: " . mysql_error());
if (isset($_POST['delete']) && isset($_POST['game_id']))
{
$game_id = get_post('game_id');
$query = "DELETE FROM games WHERE game_id='$game_id'";
if (!mysql_query($query, $db_server))
echo "DELETE failed: $query<br />" .
mysql_error() . "<br /><br />";
}
if (isset($_POST['game_id']) &&
isset($_POST['game_name']) &&
isset($_POST['game_price']) &&
isset($_POST['rating']) &&
isset($_POST['game_year']) &&
isset($_POST['description']))
{
$game_id = get_post('game_id');
$game_name = get_post('game_name');
$game_price = get_post('game_price');
$rating = get_post('rating');
$game_year = get_post('game_year');
$description = get_post('description');
$query = "INSERT INTO games VALUES" .
"('$game_id', '$game_name', '$game_price', '$rating', '$game_year', '$description')";
if (!mysql_query($query, $db_server))
echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}
echo <<<_END
<form action="sell.php" method="post"><pre>
ID: <input type="text" name="game_id" />
Name: <input type="text" name="game_name" />
Price: <input type="text" name="game_price" />
Rating <input type="text" name="rating" />
Year: <input type="text" name="game_year" />
Description: <input type="text" name="description" />
<input type="submit" value="ADD RECORD" />
</pre></form>
_END;
$query = "SELECT * FROM games";
$result = mysql_query($query);
if (!$result) die ("Database access failed: " . mysql_error());
$rows = mysql_num_rows($result);
for ($j = 0 ; $j < $rows ; ++$j)
{
$row = mysql_fetch_row($result);
echo <<<_END
<pre>
ID: $row[0]
Name: $row[1]
Price: $row[2]
Rating: $row[3]
Year: $row[4]
Description: $row[5]
</pre>
<form action="sell.php" method="post">
<input type="hidden" name="delete" value="yes" />
<input type="hidden" name="game_id" value="$row[0]" />
<input type="submit" value="DELETE RECORD" /></form>
_END;
}
mysql_close($db_server);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
<h4>Wow, low prices!</h4>
</div>
<div id="left">
<div id="left_top"></div>
<div class="leftbg">
<h3>Buy Games</h3>
<ul>
<li><a href="#"> </a></li>
<li><a href="#">Xbox 360</a></li>
<li><a href="#">Xbox</a></li>
<li><a href="#">Play Station 3</a</li>
<li><a href="#">Play Station 2</a></li>
<li><a href="#">Nintendo Wii</a></li>
<li><a href="#">Nintendo GameCube</a></li>
<form action="search.php" method="post">
Search Games: <input type="text" name="term" />
<input type="Submit" name="submit" value="Submit" />
</ul>
</div>
<div class="leftbg">
<h3>News</h3>
<div class="comnews">
<a href="#">June-17-2010</a>
<h2>Starcraft 2: Wings of Liberty</h2>
<img src="images/pic01.jpg" alt="" title="" style=" float:right; padding-right: 20px; padding-left:15px;"/>
<p align=>The StarCraft® II beta is coming! If you're interested in participating, you need to sign up for a Battle.net account, opt in to participate in upcoming ...</p>
<div class="read"><a href="http://us.starcraft2.com/beta-faq.xml">read more</a></div>
</div>
<div class="lefthr"></div>
<div class="comnews">
<a href="#">June-17-2010</a>
<h2>Call of Duty: Black Ops</h2>
<img src="images/pic02.jpg" alt="" title="" style=" float:right; padding-right: 20px; padding-left:15px;"/>
<p>Official site. Includes trailer, wallpapers, features and fan-site links for each game.</p>
<div class="read"><a href="sell.html">read more</a></div>
</div>
</div>
</div><div style="clear:both;">
</div>
<div id="footer">
<p></p>
</div></div>
</div>
</body>
</html>