Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP MySQL listing not working ...

Status
Not open for further replies.

d0nny

IS-IT--Management
Dec 18, 2005
278
GB
Hi

Not sure if this is a PHP question or a mySQL question, but here goes.
As can be guessed from my files and my issue, I'm quite new to PHP and especially to linking to a mySQL datebase.
But basically I have a News database which I have added a form on my website for users to add news stories. These are then displayed using some PHP scripting.

The issue I have is that I can then list all the news stories with an linked ID, which should then open another form to allow the user to edit the item.

Heres the form to display all the items:
Code:
<html>
<body>


<?php
   // listing script
   
   // connect to the server
   mysql_connect( 'localhost', 'username', 'password' )
      or die( "Error! Could not connect to database: " . mysql_error() );
   
   // select the database
   mysql_select_db( 'db_name' )
      or die( "Error! Could not select the database: " . mysql_error() );
   
   // retrieve all the rows from the database
   $query = "SELECT * FROM `news` ORDER BY `ID`";
   
   $results = mysql_query( $query );

   // print out the results
   if( $results )
   {
      while( $news = mysql_fetch_object( $results ) )
      {
         // print out the info
         $ID = $news -> ID;
         $StartDate = $news -> StartDate;
         $EndDate = $news -> EndDate;
		 $Section = $news -> Section;
		 $Title = $news -> Title;
		 $Summary = $news -> Summary;
		 $Content = $news -> Content;
		 $Year = $news -> Year;

         echo( "<a href='details.php?ID=$ID'>$ID</a>, $Title, $Summary<br /><br />" );
      }
   }
   else
   {
      die( "Trouble getting News from database: " . mysql_error() );
   }
   
?>

</body>
</html>

The details.php file is the following:
Code:
<html>
<head>
<title>Edit Items</title>
</head>

<body>
<?php
   // listing script
   
   // connect to the server
   mysql_connect( 'localhost', 'username', 'password' )
      or die( "Error! Could not connect to database: " . mysql_error() );
   
   // select the database
   mysql_select_db( 'db_name' )
      or die( "Error! Could not select the database: " . mysql_error() );
	  
   // get the id from the URL request
   $ID = $_REQUEST['ID'];
   
   // retrieve all the rows from the database
   $query = "SELECT * FROM `news` WHERE `ID`='$ID'";
   
   $results = mysql_query( $query );

   // print out the results
   if( $result && $news = mysql_fetch_object( $result ) )
   {
         // print out the info
         $ID = $news -> ID;
	$DateEntry = $news -> DateEntry;
         $StartDate = $news -> StartDate;
         $EndDate = $news -> EndDate;
		 $Section = $news -> Section;
		 $Title = $news -> Title;
		 $Summary = $news -> Summary;
		 $Content = $news -> Content;
		 $Year = $news -> Year;
		 
		 ?>
		 
		<table border="2" cellpadding="2" cellspacing="0">
		<tr>
		<th align="left">ID</th>
		<th align="left">Start Date</th>
		<th align="left">End Date</th>
		<th align="left">Section</th>
		<th align="left">Title</th>
		<th align="left">Summary</th>
		<th align="left">Content</th>
		<th align="left">Year</th>
		</tr>
		
		<tr>
		<td align="left"><?php echo($ID) ?></td>
		<td align="left"><?php echo($StartDate) ?></td>
		<td align="left"><?php echo($EndDate) ?></td>
		<td align="left"><?php echo($Section) ?></td>
		<td align="left"><?php echo($Title) ?></td>
		<td align="left"><?php echo($Summary) ?></td>
		<td align="left"><?php echo($Content) ?></td>
		<td align="left"><?php echo($Year) ?></td>
		</tr>
        </table>
		 		 
		 <?php
		 
	}
   else
   {
      die( "Error: Could not get News from database. ".mysql_error() );
   }
   
?>

</body>
</html>

The issue is when I select one of the ID's from the list I display, the resultant page simply states "Error: Could not get News from database."

The URL looks OK:
Any ideas? I just feel there is something really simply wrong with this and I can't see it.

Thanks ....
 
Does the behavior change if you change:

if( $result && $news = mysql_fetch_object( $result ) )

to read:

if( $result && ($news = mysql_fetch_object( $result ) ) )

?



Want the best answers? Ask the best questions! TANSTAAFL!
 

No, this didn't make any difference.
Same error message ...
 
<aside>
I recommend against using $_REQUEST, as you do in this line:

$ID = $_REQUEST['ID'];

Use $_GET, $_POST, etc as appropriate instead. $_REQUEST is vulnerable to the kind of variable poisoning that register_globals is set to "off" to protect against. (See: </aside>


Are the links produced by this line in the first script:

echo( "<a href='details.php?ID=$ID'>$ID</a>, $Title, $Summary<br /><br />" );

well-formed? Do they have all the right values in them?



If in the second script just after this line:

$query = "SELECT * FROM `news` WHERE `ID`='$ID'";

do a print:

print $query;

Does the query look right?



Want the best answers? Ask the best questions! TANSTAAFL!
 
Code:
[red]$results[/red] = mysql_query( $query );

   // print out the results
   if( [red]$result[/red] && $news = mysql_fetch_object( $result ) )


You assign the variable $results (plural) the value that mysql_query returns, however you the procede to check a variable $result (singular) that has not been initialized before. You are checking the wrong variable. check $result[red]s[/red] in your If statement.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
No prob. it happens to the best of us.

It's those kind of errors that drive crazy for hours, i know its happened to me.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita

Excellent!! That's fixed it.
I knew it must have been something really simple. As you say, something that was driving me crazy for about 2 hours!
Thanks again ...
 
You're very welcome.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top