Forum Moderators: coopster

Message Too Old, No Replies

Validation

         

galileo5

2:45 pm on Jul 23, 2006 (gmt 0)

10+ Year Member



Hello. Would someone be able to tell what is wrong with the code below? I'm trying to generate links as I input data into a form. I copied/pasted this from another site with slight modification:


<?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.

le_gber

8:33 am on Jul 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



how do you call the code on your page? do you have any echo $myarticle between <body> and </body>?

galileo5

1:17 am on Jul 25, 2006 (gmt 0)

10+ Year Member



Hello, le_gber.

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.

le_gber

7:52 am on Jul 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there at the moment title and $id do not have assigned values from the query result.

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>";
}

...

le_gber

8:00 am on Jul 25, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking at your code again the original function (message #1) was handling 2 things:
  • the articles' list
  • showing the selected article

    What you trimmed will actually require you to have two pages - one for the list and one for the article - it's not wrong, it's just another way of doing it.

  • galileo5

    2:40 am on Jul 27, 2006 (gmt 0)

    10+ Year Member



    I played around with the coding a bit until it worked. I appreciate the help you've provided, le_gber.

    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.

    le_gber

    8:41 am on Jul 27, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    sure, you don't tell your script which article to display

    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

    galileo5

    8:05 pm on Jul 29, 2006 (gmt 0)

    10+ Year Member



    Still no luck. It doesn't recognize the function WHERE colid=$myID. If I take that out, everything in my database displays; if I leave it in, no articles display.

    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.

    le_gber

    7:13 am on Jul 31, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    $result = mysql_query("SELECT * FROM news WHERE colid=$myID");

    your database column does not appear to be called colid but id. Try replacing this.

    If you could also show me the code for your page news_list it would help.

    galileo5

    11:56 pm on Jul 31, 2006 (gmt 0)

    10+ Year Member



    I had two mistakes in my coding:

    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!

    le_gber

    3:24 pm on Aug 4, 2006 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Hi Galileo5

    No worries, I've been glad to be able to help.

    Leo