Jump to content

News Script


Ne.OnZ

Recommended Posts

Hello Again! I'm trying to make a news script and was wondering if there is a better way to do it then how I have it here:

 

<?php

$news = $_POST['news'];

if($_POST['sub']) {

file_put_contents("test.php", $news, FILE_APPEND);
$file = fopen("test.php", "r+") OR exit("Error");
while(!feof($file)) {
    echo fgets($file);
}
}

if(!$_POST['sub']) {

$file = fopen("test.php", "r+") OR exit("Error");
while(!feof($file)) {
    echo fgets($file);
}
}

?>

<form action="#" method="post">
<textarea id="news" name="news" rows="8" cols="28"></textarea>
<input type="submit" id="sub" name="sub" />
</form>

 

I didn't really want FILE_APPEND, but I saw no other functions in w3 schools to make them appear in descending order. Also, should I store them in MYSQL instead of a text file? Any help would greatly be appreciated!

 

Thank You!  :) :)

Link to comment
https://forums.phpfreaks.com/topic/107562-news-script/
Share on other sites

Untested

 

table: news
+----------------------+-----------------+
|id INT AUTO_INCREMENT | news TEXT ASCII |
+----------------------+-----------------+
PRIMARY KEY (id)

 

<?php
# Init MySQL Connection here

$news = mysql_real_escape_string($_POST['news']);

if($_POST['sub'])
{
    if(mysql_query("INSERT INTO news (news) VALUES ({$news})"))
    {
        while($news_items = mysql_fetch_array(mysql_query("SELECT news FROM news WHERE id > 0 ORDER BY id desc")))
        {
            echo $news_items['news']."<hr>\n";
        }
    }
}
else
{
    while($news_items = mysql_fetch_array(mysql_query("SELECT news FROM news WHERE id > 0 ORDER BY id desc")))
    {
        echo $news_items['news']."<hr>\n";
    }
}

?>

<form action="#" method="post">
<textarea id="news" name="news" rows="8" cols="28"></textarea>
<input type="submit" id="sub" name="sub" />
</form>

Link to comment
https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551342
Share on other sites

Ok, I tried it out, and I get this error:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/divnxn5/public_html/members.php on line 16

 

<?php

include("Connect.php");
$news = mysql_real_escape_string($_POST['news']);

if($_POST['sub'])
{
if(mysql_query("INSERT INTO news(news) VALUES ({$news})"))
{
     while($news_items = mysql_fetch_array(mysql_query("SELECT news FROM news WHERE id > 0ORDER BY id DESC")))
     {
	echo $news_tems['news'] . "<br />";
      }
}
else
{
     while($news_items = mysql_fetch_array(mysql_query("SELECT news FROM news WHERE id > 0ORDER BY id DESC")))
     {
	echo $news_tems['news'] . "<br />";
      }
}
}
?>

<form action="#" method="post">
<textarea id="news" name="news" rows="8" cols="28"></textarea>
<input type="submit" id="sub" name="sub" />
</form>

 

I'm guessing I would need to put mysql_fetch_array into a variable and have OR die(). Am I wrong here?

Link to comment
https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551355
Share on other sites

Yes, I made my reply after fixing that.  ;)

 

Here's the code:

 

<?php

include("includes/Connect.php");
$news = mysql_real_escape_string($_POST['news']);

if($_POST['sub'])
{
if(mysql_query("INSERT INTO news(news) VALUES ({$news})"))
{
     while($news_items = mysql_fetch_array(mysql_query("SELECT news FROM news WHERE id > 0 ORDER BY id DESC")))
     {
	echo $news_tems['news'] . "<br />";
      }
}
else
{
     while($news_items = mysql_fetch_array(mysql_query("SELECT news FROM news WHERE id > 0 ORDER BY id DESC")))
     {
	echo $news_tems['news'] . "<br />";
      }
}
}
?>

<form action="#" method="post">
<textarea id="news" name="news" rows="8" cols="28"></textarea>
<input type="submit" id="sub" name="sub" />
</form>

 

As I stated before, it wont insert into the db. I checked over and over to see if everything was correct, and I see no problems with the table name, or db name.

Link to comment
https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551653
Share on other sites

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.