Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 14 of 14
  1. #1
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Trying to fix main/sub category bugs

    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. Go ahead and give it a test-drive and see why you can't upload?

    PHP Code:
    <?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%" 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!
    Last edited by Inigoesdr; 03-04-2008 at 09:11 PM. Reason: Please use [php] tags for your code.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • #3
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    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.

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Well... get the HTML to validate first, 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.

  • #5
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    Well... get the HTML to validate first, 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!

  • #6
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Remove these extra parenthesis for a start:
    PHP Code:
    $result=mysql_query(("select * from pg_subcat where cat_id = $main_cat")); 

  • Users who have thanked Inigoesdr for this post:

    wkheathjr (03-04-2008)

  • #8
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Inigoesdr View Post
    Remove these extra parenthesis for a start:
    PHP Code:
    $result=mysql_query(("select * from pg_subcat where cat_id = $main_cat")); 
    Changed to:
    PHP Code:
    $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

  • #9
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Always test every query to see if it succeeded.

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


  • #10
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    Always test every query to see if it succeeded.

    PHP Code:
    $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.

  • #11
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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.

  • Users who have thanked Fumigator for this post:

    wkheathjr (03-04-2008)

  • #12
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Fumigator View Post
    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....

  • #13
    New Coder
    Join Date
    Jul 2006
    Posts
    14
    Thanks
    3
    Thanked 0 Times in 0 Posts
    It works fine with Global on... seems like a bad idea?? Any suggestion?

  • #14
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Yes, I suggest you use the $_POST and $_GET arrays instead of variables that only exist when register_globals is on.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •