Using 'a hrefs' to access content in database (Newbie)

Hi all,

First of all, I’m sure this is an embarrassingly easy issue so I’m sorry for wasting your time with it if it is. I have checked through many forums and googled for hours and still don’t seem to have the answer so I don’t feel so bad for asking.

I’ve just finished the venerable sitepoint guide to building a php database and am trying to apply some of those ideas to a real site I’ve designed…and have fallen at the first hurdle…!

I have made a database with a very simple table - ‘basicelements’ (six rows all with stipulated ids and designated content, ‘basiccontent’.

What I want to do is get a link from my css menu to call up the ‘basiccontent’ from a specified row in the database and display that in the content area of my front page. It’s a simple site which would have been done with static .html pages in the past but I thought this would be a nice way to start off using php. This way I have one html.php page, an index.php page and a .css page. The menu items on the html.php page relate to a row of data in the ‘basicelements’ page which then get brought up as content in the main page. Simple right? Evidently not for me. This is what I have so far, any help would be much appreciated.

php:

if (isset($_GET[‘home’]))
{
$sql = “SELECT basiccontent FROM basicelements WHERE id = ‘1’”;
$result = mysqli_query($link, $sql);

if (!$result)
	{
	$error = 'Error fetching content for Home Page';
	include 'error.html.php';

exit();
}

header('location: .');
exit();

}

while ($row = mysqli_fetch_array($result))
{
$basiccontent = $row[‘basiccontent’];
}

include ‘page.html.php’;

?>


html:

<body bgcolor=“#ffffff” text=“#000000”>
<div id=“headerimage”>
<div>
<ul id=“css3menu”>

		&lt;li class="topfirst"&gt;&lt;a href="?home" title="home"&gt;home&lt;/a&gt;
		&lt;/li&gt;
		
		
		&lt;li class="topitem"&gt;&lt;a href="?aboutus" title="about"&gt;about us&lt;/a&gt;
			&lt;ul&gt;
				&lt;li class="subfirst"&gt;&lt;a href="?advertising" title="advertising"&gt;advertising&lt;/a&gt;&lt;/li&gt;
			&lt;/ul&gt;
		&lt;/li&gt;
		
		&lt;li class="topitem"&gt;&lt;a href="#" title="journals"&gt;&lt;span&gt;journals&lt;/span&gt;&lt;/a&gt;
			&lt;ul&gt;
				&lt;li class="subfirst"&gt;&lt;a href="#" title="2010"&gt;2010&lt;/a&gt;&lt;/li&gt;
				&lt;li&gt;&lt;a href="#" title="2009"&gt;2009&lt;/a&gt;&lt;/li&gt;
				&lt;li&gt;&lt;a href="#" title="2008"&gt;2008&lt;/a&gt;&lt;/li&gt;
				&lt;li&gt;&lt;a href="#" title="2007"&gt;&lt;span&gt;2007&lt;/span&gt;&lt;/a&gt;

					&lt;ul&gt;
						&lt;li class="subfirst"&gt;&lt;a href="#" title="winter07"&gt;winter&lt;/a&gt;&lt;/li&gt;
						&lt;li&gt;&lt;a href="#" title="autumn07"&gt;autumn&lt;/a&gt;&lt;/li&gt;
						&lt;li&gt;&lt;a href="#" title="summer07"&gt;summer&lt;/a&gt;&lt;/li&gt;
						&lt;li&gt;&lt;a href="#" title="spring07"&gt;spring&lt;/a&gt;&lt;/li&gt;
					&lt;/ul&gt;

				&lt;/li&gt;
				&lt;li&gt;&lt;a href="#" title="archive"&gt;&lt;span&gt;archive&lt;/span&gt;&lt;/a&gt;
					&lt;ul&gt;
						&lt;li class="subfirst"&gt;&lt;a href="#" title="archive 0"&gt;archive 0&lt;/a&gt;&lt;/li&gt;
					&lt;/ul&gt;

				&lt;/li&gt;
			&lt;/ul&gt;
		&lt;/li&gt;
		
		&lt;li class="topitem"&gt;&lt;a href="?submissions" title="submissions"&gt;submissions&lt;/a&gt;
		&lt;/li&gt;
		
		&lt;li class="toplast"&gt;&lt;a href="?contactus" title="contact us"&gt;contact us&lt;/a&gt;
		&lt;/li&gt;
		
	&lt;/ul&gt;
&lt;/div&gt;

</div>
<div id=“bodycontent”>
<tr>
<td><?php echo $basiccontent; ?></td>
</td>
<td>column one
</td>
</tr>
</div>
</body>

I had to leave this for a bit because it was driving me nuts. In true form, coming back to it, using what you gave me, it now works. Thank you so much for taking the time to reply. :cool:

Thank you both very much for your replies. I’m sorry that I wasn’t able to reply sooner. As for error messages, I’m not getting any: either the changes I make to the php break the page or it loads fine - i.e. the banner and menu load - but when I click on the home ‘button’ the page refreshes with no content change.

Also, thank you very much for the code advice. I have made the changes but, again, for some reason the page simply refreshes without any content.

Is this the right way to echo the content in the html page given the change of code you suggest?

&lt;tr&gt;
	&lt;td&gt;&lt;?php echo $basiccontent; ?&gt;&lt;/td&gt;
	&lt;/td&gt;
	&lt;td&gt;column one
	&lt;/td&gt;
&lt;/tr&gt;

you don’t need this piece of code anymore because you already ‘kill’ the script here if(!$result){


header('location: .');
exit();

I assumed that the default content has an ID=1
just change to whatever it actually is
if there’s no default then your script will ‘die’ on the first view when $_GET[‘id’] is not set

$id = isset($_GET['id']) ? (int) $_GET['id'] : 1;

pls try this now


$id = isset($_GET['id']) ? (int) $_GET['id'] : 1;
$sql = "SELECT basiccontent FROM basicelements WHERE id = '" . mysql_real_escape_string($id) . "'"; 
$result = mysqli_query($link, $sql);
if(!$result){
	$error = 'Error fetching content for Home Page';
	include 'error.html.php';
	exit();
}

//your other codes goes here
//while...

just fill this in with correct ids


<a href="index.php?id=1" title="home">
<a href="index.php?id=2" title="aboutus">
<a href="index.php?id=3" title="advertising">



$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
if ($id)
{
$sql = "SELECT basiccontent FROM basicelements WHERE id = '" . mysql_real_escape_string($id) . "'";

What errors are you experiencing?