Jump to content

[SOLVED] PHP News Posting Application....


vapour_

Recommended Posts

Hey,

 

Ok, I have been trying to create a news posting app for my website which involves retrieving 4 values from a table and sticking it into a div tag to be displayed on the main page. I am quite new to PHP and MySQL stuff so my script is messy and probably not even close to correct. Some help/advise would be really helpful.

 

Heres the code at the top of the page:

 

<?php 
include("db_connect.php"); 
$query = "SELECT * FROM newsPosts ORDER BY timestamp;";
$result = mysql_query($query) or die ( mysql_error() );
?>

 

Heres the code for displaying the news:

 

<div class="post">
        <h1><?php echo $result["title"]; ?></h1>
        <div class="post_body">
          <p><?php echo $result["message"]; ?></p>
        </div>
        <div class="post_info">
          <div class="author"><?php echo $result["author"]; ?></div>
          <div class="timestamp"><?php echo $result["timestamp"]; ?></div>
        </div></div>

 

 

The code i used to get the information into the db table is on another page and consists of:

 

<?php 
include("db_connect.php"); 
$title = $_POST['title'];
$message = $_POST['message'];
$author = $_POST['author'];
$query = "INSERT INTO newsPosts VALUES ('".$title."', '".$message."', '".$author."', CURRENT_TIMESTAMP)";
$result = mysql_query($query) or die ( mysql_error() );
?>

 

The variables $title, $message and $author are posted in from a form on another page.

 

If i have made a stupid mistake please forgive me. Thanks in advance for the help.

Link to comment
https://forums.phpfreaks.com/topic/108944-solved-php-news-posting-application/
Share on other sites

yes you have made a stupid mistake.  you didn't fetch the result set from the resource object

here is the correction:

Heres the code at the top of the page:

<?php 
include("db_connect.php"); 
$query = "SELECT * FROM newsPosts ORDER BY timestamp;";
$result = mysql_query($query) or die ( mysql_error() );
//fetching result set
while ($row = mysql_fetch_assoc($result)){
//Heres the code for displaying the news:
?>
<div class="post">
        <h1><?php echo $row["title"]; ?></h1>
        <div class="post_body">
          <p><?php echo $row["message"]; ?></p>
        </div>
        <div class="post_info">
          <div class="author"><?php echo $row["author"]; ?></div>
          <div class="timestamp"><?php echo $row["timestamp"]; ?></div>
        </div></div>
<?
}
?>

 

for more information

http://www.php.net/manual/en/function.mysql-query.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.