Problem with Coding: elseif statements

This is in regards to the admin functions page that is a part of the simpet site I’m making as php practice (the pets are called adoptables, as an fyi). I’m a pretty green novice lol so I’ve run into a problem that I need some help with.

Most of the admin functions I have completed so far work like a charm. But I’m stuck where you upload the picture of the pet. I can’t get the form where you upload the picture from to display when you click the link and the form itself does not work if you go directly to it by URL (…/admin.php?uploada=1).

Originally I made a separate page for uploading the image and that works fine, but when I tried to consolidate all admin functions onto admin.php, it stopped working.

Here’s the code for admin.php. Since the actual document is quite long, I’ve left out the coding for the functions that work, leaving the brackets empty. If you think this code is relevant, let me know and I’ll add it in.

In addition, I don’t know if it matters or not, but I did make sure relevant variable names were unique to the document.

<?php
// Page only for admin viewable, page for editting users
include_once("config.php");
include_once("lang/lang_".$lang.".php");
$pml_title = $site_name;
include("htmltop.php");
include_once("connect.php");
// Only for admins
require("safe_admin.php");

if(isset($_GET['viewu'])) { //view list of users
    //this function works great
}elseif(isset($_GET['viewa'])) { //view list of adoptables
    //this function works great
}elseif(isset($_GET['edit'])) { // Edit user
   //this function works great
}elseif(isset($_GET['del'])) { // Delete user
   //this function works great
}elseif(isset($_GET['edita'])) { // Edit adoptable
   //this function works great
}elseif(isset($_GET['dela'])) { // Delete adoptable
   //this function works great

//this is where my problem begins, the numbered comments are
//how i keep track of brackets while coding
}elseif(isset($_GET['uploada'])) { //1
 // Upload adoptable image
 if(is_numeric($_GET['uploada'])) { //2
 if ($_FILES) { //3
 $upid = $_POST['aid'];
 $filename = $_FILES['image']['name'];
 move_uploaded_file($_FILES['image']['tmp_name'], "pets/".$upid.".jpg") or die ("Query failed: ".mysql_error());
 echo "The image uploaded successfully.<br><br><a href=\\"admin.php\\">&laquo; Back to admin</a>";
 }else{ //3/4
?>

The image file you upload will be renamed with the ID # of the adoptable.<br><br>
Please insert the ID # of the adoptable you would like to upload an image for and then select the file you would like to upload. Please make sure it is in JPEG format (.jpg).<br><br>
<form name='uploada' method='post' action='admin.php' enctype='multipart/form-data'>
<table class="center">
<tr><td>ID #: </td><td><input type='text' name='aid' /></td></tr>
<tr><td>Image: </td><td><input type='file' name='image' /></td></tr>
<tr><td><input type='submit' value='Upload' /></td><td></td></tr>
</table>
</form>
<?php
   }//4
 } //2
}else{ //1/
 // Choselist
 ?>
 <?= $admin_whatdo ?><br /><br>

<table><tr>
<td class="whatdo">Users</td><td class="whatdo">Adoptables</td><td class="whatdo">Items</td></tr>
<tr>
<td class="info">
 <ul>
  <li><a href="admin.php?viewu=do">View users</a></li>
  <li><a href="admin.php?edit=do">Edit user</a></li>
  <li><a href="admin.php?del=do">Delete user</a></li>
 </ul>
</td>
<td class="info">
<ul>
  <li><a href="admin.php?viewa=do">View adoptables</a></li>
  <li><a href="adoptable.php">Register adoptable</a></li>
  <li><a href="admin.php?uploada=do">Upload adoptable image</a></li>
  <li><a href="admin.php?edita=do">Edit adoptable</a></li>
  <li><a href="admin.php?dela=do">Delete adoptable</a></li>
</ul>
</td>
</tr></table>
 <?
}
  

include("htmlbottom.php");
?>

And now just for reference, this is the coding for the separate page I made where the uploading works just fine and everything is peachy keen. The variable names may be different from above because I changed a few to make sure they were unique to admin.php.

<?php
// Page only for admin viewable, page for uploading images

include_once("config.php");
include_once("lang/lang_".$lang.".php");
$pml_title = $site_name;
include("htmltop.php");
include_once("connect.php");
// Only for admins
require("safe_admin.php");

if ($_FILES) {
$id = $_POST['aid'];
$filename = $_FILES['image']['name'];
move_uploaded_file($_FILES['image']['tmp_name'], "pets/".$id.".jpg") or die ("Query failed: ".mysql_error());
echo "The image uploaded successfully.<br><br>";
}
?>

The image file you upload will be renamed with the ID # of the adoptable.<br><br>
Please insert the ID # of the adoptable you would like to upload an image for and then select the file you would like to upload. Please make sure it is in JPEG format (.jpg).<br><br>
<form method='post' action='adoptableu.php' enctype='multipart/form-data'>
<table>
<tr><td>ID #: </td><td><input type='text' name='aid' /></td></tr>
<tr><td>Image: </td><td><input type='file' name='image' /></td></tr>
<tr><td><input type='submit' value='Upload' /></td><td></td></tr>
</table>
</form>

<?php include("htmlbottom.php") ?>

Any help on what I’m doing wrong would be appreciated :slight_smile: If more info is needed or whatever, please just let me know.

You’ll probably find it much easier to use indentation of the code blocks, rather than using numbered comments. Most people find it makes the code much easier to understand.

For example



}elseif(isset($_GET['uploada'])) { //1
// Upload adoptable image
    if(is_numeric($_GET['uploada'])) { //2
        if ($_FILES) { //3
            $upid = $_POST['aid'];
            $filename = $_FILES['image']['name'];
            move_uploaded_file($_FILES['image']['tmp_name'], "pets/".$upid.".jpg") or die ("Query failed: ".mysql_error());
            echo "The image uploaded successfully.<br><br><a href=\\"admin.php\\">&laquo; Back to admin</a>";
        }else { //3/4
            ?>

The image file you upload will be renamed with the ID # of the adoptable.<br><br>
Please insert the ID # of the adoptable you would like to upload an image for and then select the file you would like to upload. Please make sure it is in JPEG format (.jpg).<br><br>
<form name='uploada' method='post' action='admin.php' enctype='multipart/form-data'>
    <table class="center">
        <tr><td>ID #: </td><td><input type='text' name='aid' /></td></tr>
        <tr><td>Image: </td><td><input type='file' name='image' /></td></tr>
        <tr><td><input type='submit' value='Upload' /></td><td></td></tr>
    </table>
</form>
            <?php
        }//4
    } //2
}else { //1/

Anyway,

the form’s html will only be output if the both of these conditions are true
isset($_GET[‘uploada’])
is_numeric($_GET[‘uploada’])

Now, at least one of them isn’t true, or the code would be doing what you expected. This is a good time for you to check and find out what your code is actually doing.

Test your conditional statements to see whats really happening when you code runs. An easy way is to just stick some code inside there that outputs something.


}elseif(isset($_GET['uploada'])) { //1
    echo 'it was set';
    if(is_numeric($_GET['uploada'])) { //2
        echo 'it was numeric';

Next, you’ll probably want to echo out a certain variable, so you can see the value of it…

I usually try to tab it and stuff but when the code gets long, even the tabbing twirls my eyes around @_@ so I took to numbering them.

And I took your advice on putting little echos into the if statements.

It prints “it is set” so the first if statement works, but the is_numeric one doesn’t.

Here’s my revised coding:

}elseif(isset($_GET['uploada'])) { //1
 // Upload adoptable image
	echo "it is set<br>";
	if(is_numeric($_GET['uploada'])) { //2
		echo "it is numeric<br>";
		if ($_FILES) { //4 checks if a file was uploaded
			echo "there's a file<br>";
			$upid = $_POST['aid'];
			$filename = $_FILES['image']['name'];
			move_uploaded_file($_FILES['image']['tmp_name'], "pets/".$upid.".jpg") or die ("Query failed: ".mysql_error());
			echo "The image uploaded successfully.<br><br><a href=\\"admin.php\\">&laquo; Back to admin</a>";
		} //4
		else echo "there is no file<br>";
	}else{ //2/3
?>

	The image file you upload will be renamed with the ID # of the adoptable.<br><br>
	Please insert the ID # of the adoptable you would like to upload an image for and then select the file you would like to upload. Please make sure it is in JPEG format (.jpg).<br><br>
	<form name='uploada' method='post' action='admin.php' enctype='multipart/form-data'>
	<table class="center">
	<tr><td>ID #: </td><td><input type='text' name='aid' /></td></tr>
	<tr><td>Image: </td><td><input type='file' name='image' /></td></tr>
	<tr><td><input type='submit' value='Upload' /></td><td></td></tr>
	</table>
	</form>
	<?php
	} //3
 }else{

I’ve been trying a bunch of different stuff for the last few hours with no success :frowning: Any points in the right direction would be appreciated :slight_smile:

var_dump($_GET[‘uploada’])

Is it numeric? Should it be? Where did it come from?

Hmm, I thought it would be because ultimately the url would read thus: admin.php?uploada=#
Where the # would be the ID # of the pet whose image is being uploaded. When the ID # is not entered, it’s admin.php?uploada=do, which is not numeric.

If there’s a better way to do it, please enlighten me :slight_smile:

Values from $_GET are either strings or arrays. You’ll be wanting something more like:


if (intval($_GET['uploada']) > 0) {

How do you mean entered? In the form? You have <li><a href=“admin.php?uploada=do”>Upload adoptable image</a></li>, so clicking that link will have $_GET[‘uploada’] == ‘do’, submitting the form will have it not set.

Yeah the ID # gets entered in the form.

In several of the other admin functions, that’s the way it works so I thought it might work here too.

Here’s an example of one where that works. When you choose the user you want to edit, the url looks like this: admin.php?edit=7

elseif(isset($_GET['edit'])) {
 // Edit user
 if(is_numeric($_GET['edit'])) {
  if(isset($_POST['submit'])) {
   // Editexec
   if($_POST['pass1'] != "") {
    if($_POST['pass1'] == $_POST['pass2']) {
     $newpass = md5($_POST['pass1']);
     $sql = "UPDATE `".$db_tbl."` SET name='".$_POST['naam']."',password='".$newpass."',mail='".$_POST['email']."',active='".$_POST['actief']."',state='".$_POST['status']."' WHERE id='".$_GET['edit']."'";
     $query = mysql_query($sql);
     if($query == TRUE) {
      echo $admin_editok;
     }else{
      echo $error;
     }
    }else{
     echo $admin_pascheck;
    }
   }else{
    // Change without pass
    $sql = "UPDATE `".$db_tbl."` SET name='".$_POST['naam']."',mail='".$_POST['email']."',active='".$_POST['actief']."',state='".$_POST['status']."' WHERE id='".$_GET['edit']."'";
    $query = mysql_query($sql);
    if($query == TRUE) {
     echo $admin_editok;
    }else{
     echo $error;
    }
   } 
  }else{
   // Editform
   $sql = "SELECT * FROM `".$db_tbl."` WHERE id='".$_GET['edit']."'";
   $query = mysql_query($sql);
   $row = mysql_fetch_object($query);
   $naam = htmlspecialchars($row-&gt;name);
   $status = htmlspecialchars($row-&gt;state);
   $email = htmlspecialchars($row-&gt;mail);
   $actief = htmlspecialchars($row-&gt;active);
   ?&gt;
&lt;table class="center"&gt;&lt;tr&gt;&lt;td&gt;
   &lt;form method="post" action="admin.php?edit=&lt;?= $_GET['edit'] ?&gt;"&gt;
    &lt;table&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;label for="naam"&gt;&lt;?= $admin_name ?&gt;:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="text" id="naam" name="naam" value="&lt;?= $naam ?&gt;" maxlength="50" /&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;label for="pass1"&gt;&lt;?= $admin_newpass ?&gt;:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="pass1" type="password" name="pass1" /&gt; &lt;small&gt;&lt;?= $admin_newpass_note ?&gt;&lt;/small&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;label for="pass2"&gt;&lt;?= $admin_repeat ?&gt;:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="pass2" type="password" name="pass2" /&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;label for="email"&gt;&lt;?= $admin_mail ?&gt;:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="email" type="text" name="email" value="&lt;?= $email ?&gt;" maxlength="100" /&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;label for="actief"&gt;&lt;?= $admin_active ?&gt;:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="actief" type="text" name="actief" value="&lt;?= $actief ?&gt;" maxlength="1" size="1" /&gt; &lt;small&gt;(&lt;?= $admin_active_note ?&gt;)&lt;/small&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;label for="status"&gt;&lt;?= $admin_state ?&gt;:&lt;/label&gt;&lt;/td&gt;&lt;td&gt;&lt;input id="status" type="text" name="status" value="&lt;?= $status ?&gt;" maxlength="1" size="1" /&gt; &lt;small&gt;(1 = admin, 0 = user)&lt;/small&gt;&lt;/td&gt;
     &lt;/tr&gt;
     &lt;tr&gt;
      &lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="submit" name="submit" value="&lt;?= $admin_save ?&gt;" /&gt;&lt;/td&gt;
     &lt;/tr&gt;
    &lt;/table&gt;
   &lt;/form&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
   &lt;?
  }
 }else{
  // List
  if(isset($_GET['search'])) {
   $search = $_GET['search'];
  }else{ 
   $search = "";
  }
  ?&gt;
  &lt;?= $admin_usesearch ?&gt;&lt;br /&gt;
  &lt;form name="search" method="get" action="admin.php"&gt;
  &lt;input type="hidden" name="edit" value="true" /&gt;
  &lt;input type="text" name="search" value="&lt;?= $search ?&gt;" /&gt;
  &lt;input type="submit" value="&lt;?= $admin_search ?&gt;" /&gt;
  &lt;/form&gt;&lt;p /&gt; 
  &lt;?= $admin_whichedit ?&gt;
  &lt;form name="select" method="get" action="admin.php"&gt;
   &lt;table&gt;
    &lt;tr&gt;
     &lt;td&gt;&lt;select name="edit" size="1"&gt;
      &lt;?
      if(isset($_GET['search'])) {
       $search = "%".$_GET['search']."%";
      }else{
       $search = "jfsLSdkjdsLD";
      }
      $sql = "SELECT name,id FROM `".$db_tbl."` WHERE name LIKE '".$search."' ORDER BY name ASC";
      $query = mysql_query($sql);
      $count = mysql_num_rows($query);
      if($count &gt; 0) {
       while($row = mysql_fetch_object($query)) {
        $id = htmlspecialchars($row-&gt;id);
        $naam = htmlspecialchars($row-&gt;name);
        echo "&lt;option value=\\"".$id."\\"&gt;".$naam."&lt;/option&gt;\
";
       }
      }else{
       echo "&lt;option value=\\"do\\"&gt;* ".$admin_noresult." *&lt;/option&gt;\
";
      }
      ?&gt;&lt;/select&gt;
     &lt;/td&gt;
     &lt;td&gt;&lt;input type="submit" value="&lt;?= $admin_edit ?&gt;" /&gt;&lt;/td&gt;
    &lt;/tr&gt;
   &lt;/table&gt;
  &lt;/form&gt;
  &lt;?
 }
}

<form name=“select” method=“get” action=“admin.php”>

You specify method = get so it comes to the $_GET array, with post it will go to $_POST. As to which one to use, think of them as get and set, respectively. When you want to fetch data use “get”, when you want to change data use “post”. But whichever, make sure you know which it should be in the receiving script.

That did the trick!! Thanks so much :smiley: It totally works now. Here’s the final coding for that part of the code:

elseif(isset($_GET['uploada'])) { //1
 // Upload adoptable image
	if (is_numeric($_GET['uploada']) > 0) { //2
		echo "Please choose a file to upload. The file will be renamed with the ID # of the adoptable. If the file already exists in the directory, the new one will overwrite the old one.<br><br>";
		if ($_FILES) { //4 checks if a file was uploaded
			$upid = $_GET['uploada'];
			$filename = $_FILES['image']['name'];
			move_uploaded_file($_FILES['image']['tmp_name'], "pets/".$upid.".jpg") or die ("Query failed: ".mysql_error());
			echo "The image uploaded successfully.<br><br><a href=\\"admin.php\\">&laquo; Back to admin</a>";
		}else{ //4
		?>
		<form method='post' action='admin.php?uploada=<?= $_GET['uploada'] ?>' enctype='multipart/form-data'>
		<table>
		<tr><td>Image: </td><td><input type='file' name='image' /></td></tr>
		<tr><td><input type='submit' value='Upload' /></td><td></td></tr>
		</table>
		</form>
		<?php }
 }else{
  // List
  if(isset($_GET['search'])) {
   $search = $_GET['search'];
  }else{ 
   $search = "";
  }
  ?>
  Search for an adoptable by filling in the name of the adoptable. To get a list of all adoptables, simply leave the field blank.<br />
  <form name="search" method="get" action="admin.php">
  <input type="hidden" name="uploada" value="true" />
  <input type="text" name="search" value="<?= $search ?>" />
  <input type="submit" value="<?= $admin_search ?>" />
  </form><p /> 
  Which adoptable would you like to upload an image for?<br>
  <form name="selectu" method="get" action="admin.php">
   <table>
    <tr>
     <td><select name="uploada" size="1">
      <?
      if(isset($_GET['search'])) {
       $search = "%".$_GET['search']."%";
      }else{
       $search = "jfsLSdkjdsLD";
      }
      $sql = "SELECT name,a_id FROM adoptables WHERE name LIKE '".$search."' ORDER BY a_id ASC";
      $query = mysql_query($sql);
      $count = mysql_num_rows($query);
      if($count > 0) {
       while($row = mysql_fetch_object($query)) {
        $aid = htmlspecialchars($row->a_id);
        $aname = htmlspecialchars($row->name);
        echo "<option value=\\"".$aid."\\">#".$aid." ".$aname."</option>\
";
       }
      }else{
       echo "<option value=\\"do\\">* No adoptables found *</option>\
";
      }
      ?></select>
     </td>
     <td><input type="submit" value="Select" /></td>
    </tr>
   </table>
  </form>
  <?
 }
}