Jump to content

Recommended Posts

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
[code]<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>[/code]
this is insertnews.php
[code]<?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();
?>[/code]
And this is the page that the content is to be displayed on.  I use a switch ($_GET['page']) to open pages
[code]<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>[/code]
Link to comment
https://forums.phpfreaks.com/topic/15043-problems-displaying-data-from-database/
Share on other sites

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.


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

I changed the form action from [code]<form action="index.php?page=insert" method="post">[/code] to [code]<form action="insert.php" method="post">[/code] 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
[code]<?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');
    }
?>[/code]If there is no way around it i'll have to stick with it
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.