Jump to content

A Buggy Loop? :(


Zoey

Recommended Posts

I've got this form, in which there is an intial 'owner' name for a pet (part of a virtual pet adoptions fansite), and people can input the adopter name into the form. If an adopter name is entered, I want the owner name to switch and become the adopter name.. but if none is entered, I want the owner name to stay the same when the form is update. However, the way it's coded curently, even though my DEBUG statements seem to be working, the owner name is /always/ becoming the adopter name. Any ideas why this is and how to fix this?

[code]
        $owner1 = $owner;

        if(!$adopter)
        { $owner = $owner1; print("DEBUG IF"); }
        else
        { $owner = $adopter; unset($adopter); print("DEBUG ELSE"); }

            $result=mysql_db_query("neopets","UPDATE pet SET ownername='$owner' WHERE id = $edit_pet;",$database);

[/code]

And the code for the two input areas of the form is:

[code]
   <tr><td>Owner:</td>
   <td><?php print $owner; ?></td></tr>
   <tr><td>Adopter Username:</td>
   <td><textarea cols="30" rows="1" wrap="soft" name="adopter"></textarea></td></tr>
[/code]
Link to comment
Share on other sites

Here is pretty much the entire coding of the form (the rest being stuff involving the layout and whatnot):

[code]
<?php
if($edit_pet)
         {
            $sexid = ($sexid != "Male" && $sexid != "Female") ? "Male" : $sexid;
            $logdata = "Pet $edit_pet edited by ";
            $logdata .= $_SESSION["username"];
            $logdata .= " status ";
            $logdata .= get_text("status",$status);
            log_event($logdata, "petedit");

        $owner1 = $owner;

        if(!$adopter)
        { $owner = $owner1; print("DEBUG IF"); }
        else
        { $owner = $adopter; unset($adopter); print("DEBUG ELSE"); }

            $result=mysql_db_query("neopets","UPDATE pet SET petname='$petname',ownername='$owner',pic_url='$pic_url',adoptdate='$cur_date',link_url='$owner_url',color='$color',species='$species',status='$status',comment='$comment' WHERE id = $edit_pet;",$database);
        if($result)
            {  
               print("\n<p><center><b><i>Pet $edit_pet modified.</i></b></center></p>\n");
            }
            else
            {
               print("\n<p><center><b><i>Modify FAILED for pet $edit_pet</i></b></center></p>\n");
            }  
         }
         else if($holdid)
         {
            $result=mysql_db_query("neopets","UPDATE pet SET status='4' WHERE (id = '$delid');",$database);
            $logdata = "Pet ".$holdid." on admin hold";
            log_event($logdata, "pethold");

            if($result)
            {
               print("\n<p><center><b><i>Pet $holdid on administrative hold!</i></b></center></p>\n");
            }
            else
            {
               print("\n<p><center><b><i>Hold FAILED for pet $holdid</i></b></center></p>\n");
            }
         }
         else if($delid)
         {
            $result=mysql_db_query("neopets","DELETE FROM pet WHERE (id = '$delid');",$database);
            $logdata = "Pet ".$holdid." deleted by ".$_SESSION["username"];
            log_event($logdata, "petdelete");

            if($result)
            {
               print("\n<p><center><b><i>Pet $delid deleted!</i></b></center></p>\n");
            }
            else
            {
               print("\n<p><center><b><i>Delete FAILED for pet $delid</i></b></center></p>\n");
            }
         }
         else if($id)
         {
            $result=mysql_db_query("neopets","SELECT petname, ownername, adoptername, pic_url, link_url, color, species, sex, status, comment FROM pet WHERE (id= '$id');",$database);
            if(!$result)
               print("\n<center>No pet selected for edit.</center>\n");
            else
            {
               if($pet_row=mysql_fetch_row($result))
               {
                  $petname=$pet_row[0];  
                  $owner=$pet_row[1];
                  $adopter=$pet_row[2];
                  $pic_url=$pet_row[3];  
                  $owner_url=$pet_row[4];  
                  $colorid=$pet_row[5];
                  $speciesid=$pet_row[6];
                  $sexid=$pet_row[7];
                  $status=$pet_row[8];
                  $comment=$pet_row[9];
               }
            }
         }
      }
      else print("\n<center><b>Invalid admin status.</b></center>");
   }
   else print("\n<center><b>Invalid session.</b></center>");
?>
<table>
<tr><td><center><b><u>Edit pet</u></b></center><br></td></tr>
<tr><td>
<form method="POST" action="pet_edit.php">
<table><tbody>
<tr><td>
   <input type="hidden" name="edit_pet" value="<?php print $id; ?>">
   Pet Name:</td><td>
   <textarea cols="30" rows="1" wrap="soft" name="petname"><?php print $petname; ?></textarea></td></tr>
   <tr><td>Owner:</td>
   <td><?php print $owner; ?></td></tr>
   <tr><td>Adopter Username:</td>
   <td><textarea cols="30" rows="1" wrap="soft" name="adopter"></textarea></td></tr>
   <tr><td>Picture URL:</td>
   <td><textarea cols="30" rows="1" wrap="soft" name="pic_url"><?php print $pic_url; ?></textarea></td></tr>
   <tr><td>Rules URL:</td>
   <td><textarea cols="30" rows="1" wrap="soft" name="owner_url"><?php print $owner_url; ?></textarea></td></tr>
   <tr><td>Color: </td><td><?php my_build_adv_listbox("neopets", "color", "id", "description","$colorid"); ?></td></tr>
   <tr><td>Species:</td><td><?php my_build_adv_listbox("neopets", "species", "id", "description","$speciesid"); ?></td></tr>
   <tr><td>Sex:</td><td><?php my_build_adv_listbox("neopets", "sex", "id", "description","$sexid"); ?></td></tr>
   <tr><td>Status:</td><td>
      <?php
         if(array_key_exists("adminflags",$_SESSION)) {
            if($_SESSION["adminflags"] > 10) {my_build_adv_listbox("neopets", "status", "id", "description","$status"); }
            else {my_build_adv_listbox("neopets", "userstatus", "id", "description","$status"); }
         }
      ?>
   </td></tr>
   <tr><td>Comment:</td><td>
   <textarea cols="30" rows="10" wrap="soft" name="comment"><?php print $comment; ?></textarea>
   <tr><td colspan=2><center><br><br><input type="submit" name="Submit" value="Modify Pet"></center></td></tr></tbody></table>
</form>
</td></tr></tbody></table>
[/code]

And this is the code that defines the functions used on that page

[code]
<?php
        function my_build_adv_listbox($database, $tablename, $valfieldname, $displayfieldname, $defaultval)
        {
                $fulltable = mysql_db_query($database,"SELECT $valfieldname, $displayfieldname FROM $tablename ORDER BY $displayfieldname ASC;");
                $tablesize = mysql_num_rows($fulltable);
                if($tablesize) //if no entries in table, do not make list.
                {
                        print ("\n<select name=\"$tablename\">");
                        for ($loopcnt=0; $loopcnt < $tablesize; $loopcnt++)
                        {
                                $cur_row=mysql_fetch_row($fulltable);
                                print ("\n<option value=\"$cur_row[0]\"");
                                if($cur_row[0]==$defaultval) print(" SELECTED");
                                print(">$cur_row[1]</option>");
                        }
                        print ("\n</select>\n");
                }
                else { print ("\n<b><i>Empty list omitted.</i></b>\n"); }
        };

        function build_listbox($database, $tablename, $valfieldname, $displayfieldname, $defaultval)
        {
                $fulltable = mysql_db_query($database,"SELECT $valfieldname, $displayfieldname FROM $tablename ORDER BY $displayfieldname ASC;");
                $tablesize = mysql_num_rows($fulltable);
                if($tablesize) //if no entries in table, do not make list.
                {
                        $retval = "\n<select name=\"$tablename\">";
                        for ($loopcnt=0; $loopcnt < $tablesize; $loopcnt++)
                        {
                                $cur_row=mysql_fetch_row($fulltable);
                                $retval .= "\n<option value=\"$cur_row[0]\"";
                                if($cur_row[0]==$defaultval) $retval .= " SELECTED";
                                $retval .= ">$cur_row[1]</option>";
                        }
                        $retval .= "\n</select>\n";
                }
                else { $retval = "\n<b><i>Empty list omitted.</i></b>\n"; }
                return $retval;
        };

        function yesno_to_bool($inval)
        {
                if(stristr($inval,"y"))
                   return "t";
                elseif(stristr($inval,"n"))
                   return "f";
                else return "NULL";
        };

        function checkbox_to_bool($inval)
        {
                if(stristr($inval,"on"))
                   return "t";
                else
                   return "f";
        };
        function get_text($tablename, $inref)
        {
           $row_data=mysql_fetch_row(mysql_db_query("neopets","SELECT description FROM $tablename WHERE id='$inref';"));
           return $row_data[0];
        }


        ?>
[/code]

If you need anything else let me know.. and thanks in advanced for your help!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.