Ne.OnZ Posted May 28, 2008 Share Posted May 28, 2008 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 More sharing options...
Prismatic Posted May 28, 2008 Share Posted May 28, 2008 I would store it in a DB, faster access and easier manipulation. Link to comment https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551338 Share on other sites More sharing options...
Prismatic Posted May 28, 2008 Share Posted May 28, 2008 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 More sharing options...
Ne.OnZ Posted May 28, 2008 Author Share Posted May 28, 2008 Thank You, I will try it out. Link to comment https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551345 Share on other sites More sharing options...
Ne.OnZ Posted May 28, 2008 Author Share Posted May 28, 2008 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 More sharing options...
Prismatic Posted May 28, 2008 Share Posted May 28, 2008 while($news_items = mysql_fetch_array(mysql_query("SELECT news FROM news WHERE id > 0ORDER BY id DESC"))) Same with the second query Link to comment https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551368 Share on other sites More sharing options...
Ne.OnZ Posted May 28, 2008 Author Share Posted May 28, 2008 Ok, the error is gone, but it won't insert. I'm pretty sure I'm selecting everything in the db correctly. Here's the layout: id int(11) auto_increment news text ascii_general_ci Thus, It doesn't echo anything. Link to comment https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551372 Share on other sites More sharing options...
darkfreaks Posted May 28, 2008 Share Posted May 28, 2008 you spelled order wrong its ORDER BY not OORDER BY Link to comment https://forums.phpfreaks.com/topic/107562-news-script/#findComment-551394 Share on other sites More sharing options...
Ne.OnZ Posted May 28, 2008 Author Share Posted May 28, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.