Hello ,
My code seems to be working well, but still theres a simple error.
OK, what i'm doing is....
I'm gonna select a topic, and click a button, it gets me the sub-topics listed.Select a sub-topic, it shows the table of realted articles to the sub-topic.
First time, when i run this form, i notice the following error:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in....
Now, i select a topic and hit the button, this error vanishes and shows me the sub-topics in the list box and when i select the sub-topic, it shows me the articles and stuff.
But still, for the first time, why does this error pop up??
All i wanna do is, the article names and article content has to be listed only after the selection of a topic and sub-topic.
Many thanks
<?php
//DB connection details
include "../dbconnection.php";
?>
<html>
<head>
<title>Delete articles</title>
</head>
<body>
<?php
// If the Change button is pressed.
if (isset($_POST["send"]))
{
// Check to see that the user selected an item from the topics.
if(isset($_POST["TOPIC_ID"]))
{
$TOPIC_ID = (int)$_POST["TOPIC_ID"];
$q = mysql_query("select * from topic where TOPIC_ID=$TOPIC_ID");
if (!mysql_num_rows($q))
$TOPIC_ID = 0;
}
else {
$TOPIC_ID = 0;
}
// If the user has selected a topic and a subtopic.
if((isset($TOPIC_ID)) && (isset($_POST["SUBTOPIC_ID"])))
{
$SUBTOPIC_ID = (int)$_POST["SUBTOPIC_ID"];
$q = mysql_query("select * from subtopic where TOPIC_ID=$TOPIC_ID and SUBTOPIC_ID=$SUBTOPIC_ID");
if (!mysql_num_rows($q))
$SUBTOPIC_ID = 0;
}
else {
$SUBTOPIC_ID = 0;
}
//If the user has selected the topic, subtopic, print the article names and article content
if ((isset($TOPIC_ID)) && (isset($SUBTOPIC_ID)))
$select_sql = "select ARTICLE_NAME, ARTICLE_CONTENT from articles where SUBTOPIC_ID=$SUBTOPIC_ID";
$result_id = mysql_query($select_sql) or die('Error retrieving records from articles.<br />Mysql Reported: '.mysql_error());
}
// If we got here, we need to display the form...
echo "<form method=post action=\"$PHP_SELF\">";
echo "<select name=TOPIC_ID>";
$q = mysql_query("SELECT * FROM topic ORDER BY TOPIC_NAME");
while ($l = mysql_fetch_array($q))
{
$selected="";
if ($l["TOPIC_ID"] == $TOPIC_ID)
$selected = "selected=1";
echo "<option value=\"".$l["TOPIC_ID"]."\" $selected>".$l["TOPIC_NAME"];
}
echo "</select><br><br>";
echo "<select name=SUBTOPIC_ID>";
if ($TOPIC_ID)
{
$q=mysql_query("SELECT * FROM subtopic WHERE TOPIC_ID=$TOPIC_ID");
while ($l=mysql_fetch_array($q))
{
$selected="";
if ($l["SUBTOPIC_ID"]==$SUBTOPIC_ID)
$selected="selected=1";
echo "<option value=\"".$l["SUBTOPIC_ID"]."\" $selected>".$l["SUBTOPIC_NAME"];
}
} else {
echo "<option value=0>Please select topic first";
}
echo "</select><br><br>";
echo " <input type=submit name=send value=\"Change\"> ";
echo "</form>";
print "<table>\n\t<tr>\n\t\t<td>Article Name</td>\n\t\t<td>Article Content</td>\n\t</tr>";
while ($r_arr = mysql_fetch_array($result_id))
print "\n\t<tr>\n\t\t<td>$r_arr[0]</td>\n\t\t<td>$r_arr[1]</td>\n\t</tr>";
print "</table>";
?>
</body>
</html>