[SOLVED] detail record from URL

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
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

[SOLVED] detail record from URL

Post by hairyjim »

HI,

Sorry Im new to this and I have a very basic question (No doubt!)

I have created a news database which holds news articles, I have created a script which returns a list of all articles and turns the title into a URL click through to read more.

Code: Select all

<a href="news_detail.php?id=".$data&#1111;'sid']."">".$data&#1111;'title']."</a>
So the URL looks similar to /news_detail.php?id=96

I want to then query the same database to bring back this record with that id number. All I get is a blank page though, what am I doing wrong?

I have the following code on the recipient page:

Code: Select all

<?php
$id = $_post&#1111;'id'];
$dbconn = mysql_connect("localhost", "username", "password");
$result = mysql_select_db("database", $dbconn);
if ( $result == false)
&#123;
		echo mysql_error();
&#125; else &#123;
		$sql = "SELECT title, maintext, cdate FROM impro_news WHERE sid = '$id'";
		$result = mysql_query( $sql  );
		if ( $result != false )
		&#123;
					$data = mysql_fetch_assoc( $result );
					echo "<table width="50%" border="0" cellspacing="0" cellpadding="0">";
  					echo "<tr>" ;
    				echo "<td width="75%">$data&#1111;title]</td>";
    				echo "<td width="25%">$data&#1111;cdate]</td>";
  					echo "</tr>";
  					echo "<tr>";
    				echo "<td colspan="2">$data&#1111;maintext]</td>";
  					echo "</tr>";
					echo "</table>";
				
		&#125; else &#123;
				echo mysql_error();
		&#125;
&#125;

?>
Last edited by hairyjim on Fri Dec 05, 2003 11:22 am, edited 1 time in total.
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

1, I'm sure it's $_GET['id'] rather than $_POST['id']

and

2, mysql_query() needs two arguements, one the query and the other the link to the database. It should be mysql_query($sql, $dbconn);

3, Arrays with text for the key should be used as $data['title'] not $data[title].

4, you can do mysql_query() or die(mysql_error()); instead of needing to have an extra line.

-Nay
Chambrln
Forum Commoner
Posts: 43
Joined: Tue Dec 02, 2003 10:45 am
Location: Oregon

Post by Chambrln »

All of what Nay said is correct, however, you can get away with only passing the SQL query to mysql_query() and not the dbconn. It may be sloppy coding, but it works.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

I ammended the code as suggested. But I still get a blank page returned, any other thoughts on this?

Code: Select all

<?php
$id = $_get&#1111;'id'];

$dbconn = mysql_connect("localhost", "username", "password");
$result = mysql_select_db("improvision", $dbconn);
if ( $result == false)
&#123;
		echo mysql_error();
&#125; else &#123;
		$sql = "SELECT sid, title, maintext, cdate FROM impro_news WHERE sid = '$id'";
		$result = mysql_query( $sql, $dbconn  );
		if ( $result != false )
		&#123;
					$data = mysql_fetch_assoc( $result );
					echo "<table width="50%" border="0" cellspacing="0" cellpadding="0">";
  					echo "<tr>" ;
    				echo "<td width="75%">$data&#1111;'title']</td>";
    				echo "<td width="25%">$data&#1111;cdate]</td>";
  					echo "</tr>";
  					echo "<tr>";
    				echo "<td colspan="2">$data&#1111;'maintext']</td>";
  					echo "</tr>";
					echo "</table>";
				
		&#125; else &#123;
				echo mysql_error();
		&#125;
&#125;

?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

not 100% sure, but i think this need to be uppercase

Code: Select all

$id = $_GET['id'];
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Orhhh man - I did not realise it is case sensitive!

To further this post, I also noticed that if I was to add ' ' round my field calls they do not show! So is what you said true Nay?
Arrays with text for the key should be used as $data['title'] not $data[title].
I only ask because I want to get things right as I am a beginner and not get the bad/wrong habits :wink:
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

weird, you should have to use ' ' around your keys

hmmm.....
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

I just get a blank record if I put '' round my fields.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

2Bech
You are absolutely correct.
PHP manual wrote: Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
on the other hand, function names isn't case-sensitive:
PHP manual wrote: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
Post Reply