PDA

View Full Version : Having Problems in Creating a Basic News System


jadepearl
06-13-2008, 02:54 PM
Hi,

Am very new in PHP but eager to learn. Am trying out this tutorial titled PHP News System from Video Design Core website.

Not to sure what I did wrong. Please bear with me if I am really long winded and uses layman terms.

According to the tutorial:

1.First you must create a database and enter this query - Done!

2.Make a new file called postnews.php - Done!
<body>
<?php
if (isset($addnews)):
?>

<FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
<p>Type the authors name here:<BR>
<input size="25" NAME="author">
<p>Type news title here:<BR>
<input size="25" NAME="newstitle">
<P>Type your news here:<BR>
<TEXTAREA NAME="newstext" ROWS=10 COLS=40 WRAP>
</TEXTAREA><BR>
<INPUT TYPE=SUBMIT NAME="submitnews" VALUE="SUBMIT">
</FORM>

<?php
else:

// Connect to the database
$dbcnx = @mysql_connect(
"localhost", "root", "root");
if (!$dbcnx) {
echo( "<P>Unable to connect to the " .
"database server at this time.</P>" );
exit();
}

// the news database
if (! @mysql_select_db("news") ) {
echo( "<P>Unable to locate the news " .
"database at this time.</P>" );
exit();
}


if ("SUBMIT" == $submitnews) {
$sql = "INSERT INTO News SET " .
"NewsText='$newstext', " .
"author='$author', " .
"NewsTitle='$newstitle', " .
"NewsDate=now()";
if (mysql_query($sql)) {
echo("<P>Your news has been added.</P>");
} else {
echo("<P>Error adding submitted news: " .
mysql_error() . "</P>");
}
}


if (isset($deletenews)) {
$sql = "DELETE FROM News " .
"WHERE ID=$deletenews";
if (mysql_query($sql)) {
echo("<P>The news has been deleted.</P>");
} else {
echo("<P>Error deleting news: " .
mysql_error() . "</P>");
}
}

echo("<P> Here are all the news " .
"in our database: </P>");

$result = mysql_query(
"SELECT ID, NewsText FROM News");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit();
}

while ( $row = mysql_fetch_array($result) ) {
$newsid = $row["ID"];
$newstext = $row["NewsText"];
echo("<P>$newstext " .
"<A HREF='$PHP_SELF?deletenews=$newsid'>" .
"Delete this News</A></P>");
}

echo("<P><A HREF='$PHP_SELF?addnews=1'>" .
"Add News!</A></P>");

endif;

?>
</body>

3. Also created another page titled news.php where the news are to be displayed - Done!

<?php
$db=mysql_connect("localhost","root","root") or die ("cant connect");
mysql_select_db("news",$db) or die ("cant change");
$news=mysql_query("SELECT * FROM News ORDER BY NewsDate DESC LIMIT 8") or die ("cant get em");
while($rows=mysql_fetch_array($news)){
$title=$rows["NewsTitle"];
$content=$rows["NewsText"];
$date=date("F d, Y H:i ",$rows["date"]);
$author=$rows["author"];
$number="100%";
printf("<table border=1 cellspacing=0 bordercolor=000000 bgcolor=555555 style=overflow:auto>");

// the title of the news
echo "<td width=300 bgcolor=#ffffff class=first><center><b>$title</b></center></td>";

// the date
echo "<td align=right bgcolor=#ffffff class=first>$date</td><tr>";

//the news itself
echo "<td colspan=2 style=overflow:auto><i>by <A href=\"mailto:noneyet\">$author</a></i><br><center>$content</center></td></table><p>";
}
?>
</body>

However, when I test it...here's the results.
When I tried to preview the postnews.php in browser (F12), this message appeared.

Unable to locate the news database at this time.

And, when I tried to preview the news.php in browser (F12), this message appeared.

cant change

What did I do wrong?
Please advice.

Thanks so much,
JP

davidj
06-13-2008, 03:53 PM
I understand you have probably worked hard at this tutorial but can i ask that you give my tutorials ago

i dont have a news tutorial but what i have is the tutorials to do most tasks which you will create your own news system

honestly you will benefit from this

plus we are here to help and offer support when you need it

the tutorials link is in my signature (The building blocks)