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\\">« 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 If more info is needed or whatever, please just let me know.