Forum Moderators: coopster
<?php
$con = mysql_connect("mysql","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
if(!isset($_GET['id']))
{
$self = $_SERVER['PHP_SELF'];
$query = "SELECT id, title FROM news ORDER BY id DESC";
$result = mysql_query($query) or die('Error : ' . mysql_error());
// create the article list
$myarticle = '<ol>';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $mytitle) = $row;
$myarticle .= "<li><a href=\"$self?id=$id\">$mytitle</a></li>\r\n";
}
$myarticle .= '</ol>';
$mytitle = 'Available Articles';
} else {
// get the article info from database
$query = "SELECT title, article FROM news WHERE id=".$_GET['id'];
$result = mysql_query($query) or die('Error : ' . mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$mytitle = $row['title'];
$myarticle = $row['article'];
}
mysql_close($con);
?>
This code doesn't seem to do anything when I apply it to my site.
Also, am I supposed to leave this line as is?
$self = $_SERVER['PHP_SELF'];
If not, what would I modify?
Thanks for your help.
No, I haven't. I just played around with it again -- this time with the echo command -- and my luck is getting better, however, I'm still short.
Here is the revised coding:
<?php
$con = mysql_connect("mysql","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
if(!isset($_GET['id']))
{
$query = "SELECT id, title FROM news ORDER BY id DESC";
$result = mysql_query($query) or die('Error : ' . mysql_error());
echo "<ol>";
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
echo "<li><a href='news_article.php?id=$id'>";
echo title;
echo "</a></li>";
}
echo "</ol>";
}
mysql_close($con);
?>
I trimmed a lot of the coding from the first post that I thought wouldn't be necessary (although I could be wrong since I'm new to this).
One of my new problems is in bold above (echo title;). I can't get the title of the article to display. If I use
$row['title']
in lieu of the bolded code, I get nothing.
The other problem I am having is that the link is not taking me to the article either. It takes me to this URL
www.mysite.net/news_article.php?id=
The ID number is not showing after the equal sign.
What would you advise?
Thanks again so much for your help.
What you would need to do is assign values to variables by adding the following in your while loop (the new code is bold blue):
...
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
$theTitle = $row['title'];
$theId = $row['id'];
echo "<li><a href='news_article.php?id=$theId'>";
echo $theTitle;
echo "</a></li>";
}
...
One more problem though. I wanted to avoid asking it b/c it seems to be such a simple command, but here it goes anyway: Once I click on the titled links, I can't get the page to display the article itself.
I know it has to do with the first of the two commands below:
$result = mysql_query("SELECT * FROM news");
while($row = mysql_fetch_array($result))
Any advice?
Thanks again.
First you need to find out which article it is, and make sur that this article is a number - because as a thoughtful developer you want your script to be as secure as possible :)
if((isset($_GET['id'])) AND (is_numeric($_GET['id']))){
$articleID = $_GET['id'];
}else{
exit('Invalid article selection');
}
Then you need to tell the select command which article it is:
$result = mysql_query("SELECT * FROM news WHERE colid = $articleID");
In here colid is the actual name of the ID column in your database.
One final comment - I assume that you want your articles to be found by the search engines. If this is the case I would avoid using the keyword id= in your urls as SE's have a history of not liking this particular one. I would either use a letter a= (a for article) or a word like article=
Hope this helps
Perhaps it would help if I showed my entire code for the page I would like to display the news:
<?php
$con = mysql_connect("mysql","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
if((isset($_GET['id'])) AND (is_numeric($_GET['id']))){
$myID = $_GET['id'];
}else{
exit('Invalid article selection');
}
$result = mysql_query("SELECT * FROM news WHERE colid=$myID");
echo "<table align='center' bgcolor='#FFFFFF' border='0' cellpadding'0' cellspacing='10' width='50%'>";
while($row = mysql_fetch_array($result))
{
echo "<tr><td valign='top' width='100%'>";
echo "<font face='arial' size='-1'><strong>";
echo $row['title'];
echo "</strong><br />";
echo $row['articleDate'];
echo "<p>";
echo $row['article'];
echo "</font>";
echo "</td>";
}
echo "</tr></table>";
mysql_close($con);
?>
And here is my insert_db page:
<?php
$con = mysql_connect("mysql","***","***");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
if (get_magic_quotes_gpc()) {
$_POST['title'] = stripslashes($_POST['title']);
}
$mytitle = mysql_real_escape_string($_POST[title]);
if (get_magic_quotes_gpc()) {
$_POST['article'] = stripslashes($_POST['article']);
}
$myarticle = mysql_real_escape_string($_POST[article]);
$mydate = date('D, j M Y g:i:sa');
$myid = ($_GET[id]);
$mynews = mysql_real_escape_string($_POST[twnews]);
$theirpics = mysql_real_escape_string($_POST[twpics]);
$sql="INSERT INTO news
(title,articleDate,article,id,twnews,twpics)
VALUES
('$mytitle','$mydate','$myarticle','$myid','$mynews','$theirpics')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Broadcasted!";
?>
Thanks again to all for the help.
1. I should have used "ID" instead of "colID" (I should have been more careful in reading your response);
2. I had an extra wavy bracket } in my code.
They've been corrected, and now the page displays the articles!
Thank you again, le_gber. You've been a great help to my project so far!