Jump to content

[SOLVED] using $_GET to query the database


parodys

Recommended Posts

Hi there,

 

A quick question,  i have a simple article website I'm putting together.  My database is set up like this;

 

# articles

id, title, article, created_at, catid

 

# category

catid, catname

 

Ive passed a catid through links as below :

 

<?php
$query = "SELECT catname, catid FROM category"; 
$result = mysql_query($query) or die(mysql_error());

// make the catogories list and then passes the values on to archive page

$entry = '<ol>';

  	  while ($row = mysql_fetch_array($result, MYSQL_NUM)) 
	{
		list($catname, $catid) = $row;
   	    	$entry .= "<li><a href=\"?view=archive&catid=" . $catid . "\">$catname</a></li>\r\n";
  	  	}

	$entry .= '</ol>';
// show the categories list
echo $entry;
?>

 

now it passes " http://localhost/codedad/?view=archive&catid=1 " on to the archive page.

 

my problem is getting the archive page to display only the articles under the catid=1.

 

below is the function ive made to display the catitems :

<?php
function displaycatitems()
		{
			db_connect();

		$catid = $_GET["catid"];

		if(!isset($_GET['catid']))
{	  
		$query ="SELECT title, article FROM articles, category WHERE cat=category.catid=".$_GET['id'];
		$result = mysql_query($query) or die('Error : ' . mysql_error());


		while($row = mysql_fetch_array($result))
			{
			echo $row['title']. " - ". $row['article'];
			echo "<br />";
			}

}
	else
	{
		echo "No Articles";
	}
}
?>

 

I know this is more than likely starting me in the face but for the last 3 hours ive been trying to get this to work to no avail!

 

Any help would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/144336-solved-using-_get-to-query-the-database/
Share on other sites

         if(!isset($_GET['catid']))
{     
         $query ="SELECT title, article FROM articles, category WHERE cat=category.catid=".$_GET['id'];
         $result = mysql_query($query) or die('Error : ' . mysql_error());
    
    
         while($row = mysql_fetch_array($result))
            {
            echo $row['title']. " - ". $row['article'];
            echo "<br />";
            }
         
   }

 

You're only running the query if $_GET['catid'] is not set;

 

!isset($_GET['catid'])

 

should be;

 

isset($_GET['catid'])

wow thanks for the quick replies !

 

Ive done what you both suggested and i get an error.

 

Notice: Undefined index: id in F:\wamp\www\codedad\dbfuncs.php on line 76

Error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND articles.catid = category.catid' at line 1

If you had a quick look you would have noticed that the wrong word was putting in for the get value ;)

 

SELECT title, article FROM articles, category WHERE category.catid=".mysql_real_escape_string($_GET['id'])." AND articles.catid = category.catid"

 

 

should be

 

<?php
SELECT title, article FROM articles, category WHERE category.catid=".mysql_real_escape_string($_GET['catid'])." AND articles.catid = category.catid"
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.