Jump to content

This time having trouble selecting from a table [MySQL]


Gyohdon

Recommended Posts

Alright, so now I can INSERT, but when I try to SELECT, it doesn't work.It just shows up blank. It echoes '(category) :', where category is either Art, or Education or whatever.But after that, where it should say 'written by', it's just blank. (And yes, I actually have something in my table, it should say 'test Written By Admin.')Edit: I have isolated problem. When looking up Art, the html stops right after the part where it echoes'</font></b></center><br />'.However, when looking up Education, the html continued and the table was created.So, I think the problem is caused by the processing of $_GET['order'], but I've got no idea why.Here's my code:

<?php	if (isset($_GET['category']))		{		if ($_GET['category'] != "")			{			if ($_GET['category'] == 'Art')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Economics')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Education')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Fashion')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Health')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'moviesandmusic')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Nature')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'News')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Politics')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Science')				{				$category = $_GET['category'];				}			elseif ($_GET['category'] == 'Technology')				{				$category = $_GET['category'];				}			}		elseif ($_GET['category'] == "")			{			unset($_GET['category']);			}		}		if (isset($_GET['category']))		{		echo '<center><b><font face="verdana" size="4">';		}		if ($category == 'Art')		{		echo "Art:";		}	elseif ($category == 'Economics')		{		echo "Economics:";		}	elseif ($category == 'Education')		{		echo "Education:";		}	elseif ($category == 'Fashion')		{		echo "Fashion:";		}	elseif ($category == 'Health')		{		echo "Health:";		}	elseif ($category == 'moviesandmusic')		{		echo "Movies & Music:";		$category = '`Movies & Music`';		}	elseif ($category == 'Nature')		{		echo "Nature:";		}	elseif ($category == 'News')		{		echo "News:";		}	elseif ($category == 'Politics')		{		echo "Politics:";		}	elseif ($category == 'Science')		{		echo "Science:";		}	elseif ($category == 'Technology')		{		echo "Technology:";		}		if (isset($_GET['category']))		{		echo '</font></b></center><br />';		}		if (isset($category))		{		$tablecreate = mysql_query("CREATE TABLE IF NOT EXISTS  `$database`.`$category` (						`ID` INT NOT NULL AUTO_INCREMENT ,						`Title` VARCHAR( 100 ) NOT NULL ,						`Name` VARCHAR( 50 ) NOT NULL ,						`Email` VARCHAR( 50 ) NOT NULL ,						`Idea` TEXT NOT NULL ,						`Responses` INT NULL ,						`Views` INT NULL ,						`Rates` INT NULL ,						PRIMARY KEY (  `ID` )						) ENGINE = MYISAM ;");				if (!$tablecreate)			{			die('Error: ' . mysql_error());			}		}			if (isset($_GET['order']))		{		if ($_GET['order'] == 'newest')			{			$result = mysql_query("SELECT * FROM `$category` ORDER BY ID DESC LIMIT 15");			}		elseif ($_GET['order'] == 'oldest')			{			$result = mysql_query("SELECT * FROM `$category` ORDER BY ID ASC LIMIT 15");			}		elseif ($_GET['order'] == 'rated')			{			$result = mysql_query("SELECT * FROM `$category` ORDER BY Rates ASC LIMIT 15");			}		elseif ($_GET['order'] == 'viewed')			{			$result = mysql_query("SELECT * FROM `$category` ORDER BY Views ASC LIMIT 15");			}		elseif ($_GET['order'] == 'discussed')			{			$result = mysql_query("SELECT * FROM `$category` ORDER BY Responses ASC LIMIT 15");			}		else			{			unset($_GET['order']);			}		}		while($row = mysql_fetch_array($result))		{		if ($category == '`movies & music`')			{			$category = 'moviesmusic';			}				echo '<a href="Idea.php?category=' . $category . '&id=' . $row['ID'] . '>			<b>' . $row['Title'] . '</b></a> <i>written by ' . $row['Name'] . '</i><br />';		}		if (!isset($_GET['category']) || !isset($_GET['order']))		{		echo '<center><font face="verdana">	<h2>Choose a category and read some ideas!</h2>	<h4>Or, if you have some of your own, post them <a href="Share.php">here</a>!</h4>	</font></center>';		}		mysql_close($con);?>

Link to comment
Share on other sites

Have you thought of consolidating your code? There's a lot of redundancy going in there. Why not clean it up to make it easier to maintain and allow for easier debugging? Have you verified the contents of $_GET to make sure order is even present? Something a little more practical might look like this:

<?phpecho var_dump($_GET); //first thing first, inspect the incoming arrayif (isset($_GET['category']) && $_GET['category'] != ""){  $category = $_GET['category'];  if ($category == 'moviesandmusic'){	$category = '`Movies & Music`';  };  echo 'category selected: ' . $category;  echo '<center><b><font face="verdana" size="4">';}else if($_GET['category'] == ""){  unset($_GET['category']);};

To which you can continue on with the lineif (isset($category))....and then you can try verifying order once a lot of the excess code isn't cluttering your script. aside from general cleanup duties, you should be echoing statements to yourself (much along the lines of the var_dump used at the beginning) to make sure values are present and expected, and that your script is executing properly.

Link to comment
Share on other sites

Have you thought of consolidating your code? There's a lot of redundancy going in there. Why not clean it up to make it easier to maintain and allow for easier debugging? Have you verified the contents of $_GET to make sure order is even present? Something a little more practical might look like this:
<?phpecho var_dump($_GET); //first thing first, inspect the incoming arrayif (isset($_GET['category']) && $_GET['category'] != ""){  $category = $_GET['category'];  if ($category == 'moviesandmusic'){	$category = '`Movies & Music`';  };  echo 'category selected: ' . $category;  echo '<center><b><font face="verdana" size="4">';}else if($_GET['category'] == ""){  unset($_GET['category']);};

To which you can continue on with the lineif (isset($category))....and then you can try verifying order once a lot of the excess code isn't cluttering your script. aside from general cleanup duties, you should be echoing statements to yourself (much along the lines of the var_dump used at the beginning) to make sure values are present and expected, and that your script is executing properly.

First thing I did. Order and Category are being present.However, the code just stops after the table creation.
Link to comment
Share on other sites

Still... let's do some cleanup... you can compress all of your ifs into a single check by simply putting all possibilities in an array, and then using in_array() to check if the submitted value is in it, like:

<?php	$categories = array('Art', 'Economics', 'Education', 'Fashion', 'Health', 'moviesandmusic', 'Nature', 'News', 'Politics', 'Science');	if (isset($_GET['category']) && !empty($_GET['category']))		{		if (in_array($_GET['category'], $categories))			{			if ($_GET['category'] == 'moviesandmusic')				{				$category = 'Movies & Music';				}			else				{				$category = $_GET['category'];				}			}		}	if (isset($category))		{			echo '<center><b><font face="verdana" size="4">' . $category . ':</font></b></center><br />';			//Rest of the code that depends on the existance of $category		}	else		{			//What happens without $category anyway?		}

Link to comment
Share on other sites

so, why do you think that is? i also suggested you alert everything as you go along. alert the values before and after each control, starting a connection, ending a connection. you are opening a connection the database, right? What is the echo value of $database?

Link to comment
Share on other sites

and...even you can cut your codes by switch..simple and cleanly...

switch($_GET['category']){case "music":case "art":case "economy://more option here as you wantcase "fashion":$category=$_GET['category'];break;default://do some error handlebreak;}

Link to comment
Share on other sites

Thanks guys, I really appreciate it, but this isn't really my problem.The category code is all working.The part where I use SELECT doesn't work.

so, why do you think that is? i also suggested you alert everything as you go along. alert the values before and after each control, starting a connection, ending a connection. you are opening a connection the database, right? What is the echo value of $database?
It's in an include file at the first line of the file.It can't be the problem, I use it on other pages too, it works fine.
Link to comment
Share on other sites

Edit: I have isolated problem. When looking up Art, the html stops right after the part where it echoes'</font></b></center><br />'.However, when looking up Education, the html continued and the table was created.So, I think the problem is caused by the processing of $_GET['order'], but I've got no idea why.
Do you have $_GET['order'] in both cases? You haven't really defined a query to be used when there's no $_GET['order'] (and $_GET['category'] for that mater) so that could be a problem.You might want to turn on error display, so that you can more clearly pinpoint the error... why not enable it in php.ini once and for all? Open it, find
error_reporting =

and make sure the line reads

error_reporting = E_ALL | E_STRICT

then find

display_errors =

and make sure the line reads

display_errors = On

Save the file, and restart Apache... try the script again and see if there are any error messages.

Link to comment
Share on other sites

I'm still wondering about order then? Did you take my advice to echo out $_GET['order'] before you use in it in the query statements? Are you checking for the value of $result each time a query is made?

Link to comment
Share on other sites

I'm still wondering about order then? Did you take my advice to echo out $_GET['order'] before you use in it in the query statements? Are you checking for the value of $result each time a query is made?
I did that before I posted here with GET-order. It echo'd just fine.Will try it with $result too.I have found the problem:The first line of HTML I echo in the while doesn't work. Can anyone explain why?OMG I GOT ITI forgot the second ' " ', in the <a> tag.Jesus, that was stupid.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...