...

View Full Version : Trying to fix main/sub category bugs



wkheathjr
03-04-2008, 07:10 PM
Hello,

I am trying to get my picture gallery up and running. This code was written by an India programmer for me few years ago and now I am starting to learn how to write PHP, but I am still in newbie with some good reading of php code.

Here is what I am trying to do is figure out why my code is not staying with main category after you pick which main category. To understand what I mean, test the live code here (http://www.mason-dixonpark.com/ncdb/postpic.php?do=fillsub). Go ahead and give it a test-drive and see why you can't upload?



<?php
include ("session.php");
?>

<script language="javascript">
function validate()
{
if(document.frmmain.main_cat.value=="")
{
alert("Select Main Category from the list.");
document.frmmain.main_cat.focus();
}
else if(document.frmupload.subcat.value=="")
{
alert("Select Sub Category from the list.");
document.frmupload.subcat.focus();
}
else if(document.frmupload.name.value=="")
{
alert("Enter name.");
document.frmupload.name.focus();
}
else
{
document.frmupload.submit();
}
}
function chkmain()
{
if(document.frmmain.main_cat.value!="")
{
document.frmmain.submit();
}
}
</script>
</head>
<?
if(isset($HTTP_POST_VARS['do']) || isset($HTTP_GET_VARS['do'])) {
$do =(isset($HTTP_POST_VARS['do'])) ? $HTTP_POST_VARS['do'] : $HTTP_GET_VARS['do'];
}
?>
<table width="100&#37;" cellspacing="0" cellpadding="10" border="0" align="center">
<tr>
<td class="bodyline">
<table class="forumline" width="100%" cellspacing="1" cellpadding="4" border="0">
<tr>
<th class="thHead" height="25"><b> Picture Gallery >> Upload Picture </b></th>
</tr>
<tr>
<td class="row1">
<div align="center">
<table width="500">
<tr>
<form name=frmmain method="post" action="?do=fillsub">
<? $result =mysql_query("SELECT * FROM pg_cat ORDER BY name"); ?>
<td>Select main category from the list</td>
<td align="center"><select name="main_cat" onchange="chkmain()">
<option value="">[Select Main Category]</option>
<? while($row =mysql_fetch_array($result)) { ?>
<OPTION value="<?=$row[0]; ?>"<?
if ($row[0]==$main_cat){
echo " selected";
}?>><?=$row[1];?>
</OPTION>
<? } ?>
</select></td>
</form>
</tr>
<form name="frmupload" enctype="multipart/form-data" method="post" action="saveupload.php">
<? if($do=="fillsub") {
$result=mysql_query(("select * from pg_subcat where cat_id = $main_cat"));
?>
<tr>
<td>Select sub category from the list</td>
<td align="center"><select name="subcat">
<option value="">[Select Sub Category]</option>
<? while($row =mysql_fetch_array($result)) { ?>
<OPTION value="<?=$row[0]; ?>"<?
if ($row[3]==$main_cat) {
echo " selected>";
} else {
echo ">";
}
echo $row[1]."</option>";
} ?>
</select></td>
</tr>
</div>
<br><br>
<div align="center">
<table width="500">
<tr>
<td align="left" valign="top">Title</td>
<td align="right"><div align="left">
<input name="name" style="width:230px" type="text"></div>
</td>
</tr>
<tr>
<td align="left" valign=top>Description</td>
<td align="right"><div align="left">
<textarea name="description" cols="35" rows="2"></textarea></div>
</td>
</tr>
<tr>
<td align="left" valign="top">Select Picture to upload</td>
<td><div align="left"><input name="fupload" style="width:300px" type="file"><br></div></td>
</tr>
<tr>
<td>&nbsp;</td>
<input type="hidden" name="main_cat" value="<?=$HTTP_POST_VARS['main_cat'];?>">
<td><div align="right">
<input id="Upload" name="Upload" type="Button" onClick="validate()" value="Upload"></div>
</td>
</tr>
</table>
<? } ?>
</form>
</td>
</tr>
</table>
</td>
</tr>
</table>


Thank for offering your help!

Fumigator
03-04-2008, 08:29 PM
First things first, fix the HTML markup code so it validates.

Next, you seem to have a problem with selecting a main category, as it reverts back to the top item when I attempt to select the test item, and that validation won't let the form submit, so the script doesn't even get to the file upload process.

wkheathjr
03-04-2008, 08:40 PM
First things first, fix the HTML markup code so it validates.

Next, you seem to have a problem with selecting a main category, as it reverts back to the top item when I attempt to select the test item, and that validation won't let the form submit, so the script doesn't even get to the file upload process.

Yep, this is why I posted my code on this forum... I can't seem to understand why it is keeping reverting back? It was not supposed to do this, and this code was working perfectly fine few years ago. I have not changed it one bit and now it is not working?

Other thing is that I forgot to say this in my first post.. is it possible to select main/sub category without using javascript? Personally I am not big on using javascript when using it to add result into the database. This could create some problems especially with some users who will fully disable the javascript in his browser.

The purpose of this is to understand how main/sub category code work (straight PHP) so I can try to practice by adding the main/sub category into simple forum and classifieds. I wanted to see if I can have a better understanding of how it work so I can immense "Manage Main/Sub Category" into my forum & classified code.

Fumigator
03-04-2008, 08:49 PM
Well... get the HTML to validate first (http://validator.w3.org/check?verbose=1&uri=http&#37;3A%2F%2Fwww.mason-dixonpark.com%2Fncdb%2Fpostpic.php%3Fdo%3Dfillsub), because that could be the whole problem. Then throw in some alert() functions in the Javascript to see if you can pinpoint the select box issues.

wkheathjr
03-04-2008, 08:50 PM
Well... get the HTML to validate first (http://validator.w3.org/check?verbose=1&uri=http%3A%2F%2Fwww.mason-dixonpark.com%2Fncdb%2Fpostpic.php%3Fdo%3Dfillsub), because that could be the whole problem. Then throw in some alert() functions in the Javascript to see if you can pinpoint the select box issues.

Okay thanks.. actually I am looking to abandon javascript anyway... thanks!

wkheathjr
03-04-2008, 08:59 PM
Ok, Here is interesting info.. I took upon Fumigator advice and looked into HTML. By doing so, I ran a live test-drive then "View Source" and I see this inside the page (not visible on webpage)


Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mdpark/public_html/ncdb/postpic.php on line 77

Inigoesdr
03-04-2008, 09:17 PM
Remove these extra parenthesis for a start:

$result=mysql_query(("select * from pg_subcat where cat_id = $main_cat"));

wkheathjr
03-04-2008, 09:55 PM
Remove these extra parenthesis for a start:

$result=mysql_query(("select * from pg_subcat where cat_id = $main_cat"));

Changed to:

$result = mysql_query("select * from pg_subcat where cat_id = $main_cat") or die(mysql_error());

Getting this message before the Main Cat selection:


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 '' at line 1

But on line 1, this is <?PHP

Fumigator
03-04-2008, 09:57 PM
Always test every query to see if it succeeded.



$query = "SELECT blah blah ";
$result = mysql_query($query);
if (!$result) {
die ("SQL error! query is $query<br />Error is ".mysql_error());
}

wkheathjr
03-04-2008, 09:59 PM
Always test every query to see if it succeeded.



$query = "SELECT blah blah ";
$result = mysql_query($query);
if (!$result) {
die ("SQL error! query is $query<br />Error is ".mysql_error());
}


Already ahead of you.. look at my post before your suggestion. :thumbsup:

Fumigator
03-04-2008, 10:09 PM
And where is $main_cat populated? Is it empty? Probably... assign your query to a variable and echo the variable to find out for sure.

Assuming it is... your script looks like it's expecting register_globals to be on, which is by default set to off anymore. This is probably your problem.

wkheathjr
03-04-2008, 10:13 PM
And where is $main_cat populated? Is it empty? Probably... assign your query to a variable and echo the variable to find out for sure.

Assuming it is... your script looks like it's expecting register_globals to be on, which is by default set to off anymore. This is probably your problem.

Ahh maybe you are right!! Global is off....

wkheathjr
03-05-2008, 01:56 AM
It works fine with Global on... seems like a bad idea?? Any suggestion?

Fumigator
03-05-2008, 02:39 AM
Yes, I suggest you use the $_POST and $_GET arrays instead of variables that only exist when register_globals is on. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum