Jump to content


Photo

Problems displaying data from database


  • Please log in to reply
3 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 19 July 2006 - 02:38 PM

I am having problems displaying data from a database onto a page.  i haven't used php for months so am getting back into it slowly.

I am sure I have been able to add a record becuase everything seems fine but the page is displaying nothing when i try to view the content.  Is there a way i can see what records exist in the database?

Here is how i insert a new record
<form action="index.php?page=insertnews" method="post">
	<p class="style1">Please enter a title for the news item.
	<input style="width:400px;" type="text" size="50" name="title"></p>
	<p class="style1">Please enter the content for the news item.
	<textarea style="width:400px;" name="content" cols="30" rows="10">
	</textarea></p>
	<input class="submit-button" style="margin-left:0"  type="Submit" value="Submit"><input class="submit-button" style="margin-left:2px" type="reset" value="Reset">
</form>
this is insertnews.php
<?php
	include_once("../includes/connection.inc");
	$title= $_POST['title'];
	$content= $_POST['content'];
	mysql_connect($host,$user,$password);
	@mysql_select_db($database) or die( "Unable to select database");
	$query = "INSERT INTO news VALUES ('','$title','$content')";
	mysql_query($query);
	mysql_close();
?>
And this is the page that the content is to be displayed on.  I use a switch ($_GET['page']) to open pages
<h3><?php	
	include_once("includes/connection.inc");
                mysql_connect($host,$user,$password);
  	   @mysql_select_db($database) or die( "Unable to select database");
	   $result = @mysql_query("SELECT title FROM news");			
	   if (!$result) {
               echo("<p>Error performing query: " . mysql_error() . "</p>");
	    exit();             
	}	
	while ( $row = mysql_fetch_array($result) ) {
	   echo("<p>" . nl2br($row["title"]) . "</p>");				}
?></h3>
<p class="style3"><?php	
	include_once("includes/connection.inc");
	$result = @mysql_query("SELECT content FROM news");			
	if (!$result) {
            echo("<p>Error performing query: " . mysql_error() . "</p>");
	    exit();             
	}
	while ( $row = mysql_fetch_array($result) ) {
	    echo("<p>" . nl2br($row["content"]) . "</p>");
	}
	mysql_close();
?></p>

If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 Ninjakreborn

Ninjakreborn
  • Members
  • PipPipPip
  • Information Technology Specialist
  • 3,922 posts
  • Age:33

Posted 19 July 2006 - 02:40 PM

is it blank, if it is then is error viewing off in php.ini if so it's syntax error, is it returning a specific error or what.

------

Business Website: http://www.infotechnologist.biz

Personal Website: http://www.joyelpuryear.com

Blog Site: http://www.realmofwriting.com
Services: Web development, application development, mobile development, and custom development. All services listed on my website.


#3 SammyP

SammyP
  • Members
  • PipPipPip
  • Advanced Member
  • 58 posts
  • LocationLondon

Posted 19 July 2006 - 04:18 PM

The Insert query looks OK but I prefer the Set syntax to specifically set the fields. So long as your table has three fields should be fine though. I take it after you see the insertnews page you either redirect to the viewing page or you are doing it manually at the moment.

Update table Set field1='sam', field2=16

Other than that I don't know why you are dereferencing the mysql_query function with the @ symbol. I don't know if there is a reason for it but I never do it. Try it without and see if that works.

Do you use phpMyAdmin or something similar to help set up and view your database?

Sam.



If this has solved your problem, a quick 'Thanks' is appreciated, but marking the thread as 'Solved' is better - it saves a lot of time.

Sam.

#4 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 20 July 2006 - 11:58 AM

I've installed php etc on my local machine and set up my database

I changed the form action from
<form action="index.php?page=insert" method="post">
to
<form action="insert.php" method="post">
and now it works  i can insert records and I can display them.

Is there no way I can use the switch statement so i can use the original form action?

Here is the switch statement on the index.php page
<?php
			switch ($_GET['page'])
    			{
       			
       			case "contact":
			include('contact.php');
			break;

			case "news":
			include('news.php');
			break;

			case "news1":
			include('news1.php');
			break;


       			default:
			include('home.php');
    			}
		?>
If there is no way around it i'll have to stick with it
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users