Log in

View Full Version : Having Problems with PHP News System Tutorial



jadepearl
06-13-2008, 03:00 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", "userID", "password");
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","userID","password") 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

boogyman
06-13-2008, 03:39 PM
For future reference, it's highly recommended that you do not post username/password/ other sensitive data when posting in a public forum for security purposes. We know what those arguments are supposed to be "username" and "password"

Please read our Policies and Regulations (http://dynamicdrive.com/forums/showthread.php?t=24866), especially #8 (posting computer code)

now as for your problem....

where is the script located?
where is the database located?

you are receiving the first error because you cannot connect to the database, which is usually caused by the mysql_connect() portion. Make sure that all of the arguments are assigned correctly, especially the first (host). If any of them are not correct.

Also by saying what "fast function" you use to preview the page, I am going to assume that you are using your local harddrive and some sort of front-end client like "Dreamweaver". This goes back to the location of the script and the database. If you are trying to test this on your local computer, you first need a few things
you need to be sure your computer has php / apache / mysql all installed and configured correctly.

jadepearl
06-13-2008, 11:03 PM
boogyman - Thanks and apologies! Have read the Policies and Regulations and will abide to it. Thanks for your input. I will check it once again.:)