Jump to content

Recommended Posts

I've started building my first admin area for a website using PHP, which I am a beginner at.

 

On one of the pages I need to edit (the home page), I have a page which lists the areas that are editable. There are two areas for text and one for an image. I can easily update the text areas and upload the text into a database for it to be inserted on the front end home page.

 

However, my problem is that the image will upload to it images folder, and update the database with the path and file name temporarily, but will not stay.

 

This is the Edit Page for the Home Page:

 

<?php
include_once '../admin-class.php';
$admin = new itg_admin();
$admin->_authenticate();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Admin Area</title>
        <script type="text/javascript" src="../../tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
        <script type="text/javascript">
        	tinyMCE.init({
        		mode : "textareas",
        		theme : "advanced",
        		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect",
        		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,forecolor,backcolor",
        		theme_advanced_resizing : true
        	});
        </script>
        <link rel="stylesheet" type="text/css" href="../../css/reset.css" />
        <link rel="stylesheet" type="text/css" href="../../css/style.css" />
        <link rel="stylesheet" type="text/css" href="../css/admin.css" />
    </head>
    <body>
    	<div id="wrapper">
        	<p>Logged in as <?php echo $_SESSION['admin_login']; ?>.</p>
        	<br />
        	<h3>HOME PAGE</h3>
        	<br />
        	<?php include 'includes/area_a.php'; ?>
        	<?php include 'includes/showcase_image.php'; ?>
        	<?php include 'includes/testimonials_text.php'; ?>
        	<br />
        	<p><a href="../index.php"><input type="submit" value="Go Back" /></a></p>
        	<br />
        	<p><input type="button" onclick="javascript:window.location.href='logout.php'" value="Log Out" /></p>
        </div>
    </body>
</html>

 

This is the include file for Area A:

 

			<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<table class="tblInclude">
				<tr>
					<td><h4>Area A:</h4></td>
				</tr>
				<tr>
        				<td>
        					<textarea name="area_a" class="txtInclude">
        						<?php
        							$con = mysql_connect("localhost","XXXXX","XXXXX");
        							if (!$con)
        							{
        								die('Could not connect: ' . mysql_error());
      					  			}
        							mysql_select_db("rainbowDB", $con);
        							$result = mysql_query("SELECT * FROM areas WHERE area='area_a'");
        							while($row = mysql_fetch_array($result))
        							{
        								echo $row['content'];
        							}
        							mysql_close($con);
        						?>
        					</textarea>
        				</td>
        			</tr>
        			<tr>
        				<td><input type="submit" value="Update" name="update_area_a" /></td>
        			</tr>
        		</table>
        	</form>
        	<?php
        		if(isset($_POST['update_area_a']))
        		{
        			$area_a = $_POST["area_a"];
        			$con = mysql_connect("localhost","XXXXXX","XXXXXX");
        			if (!$con)
        			{
        				die('Could not connect: ' . mysql_error());
        			}
        			mysql_select_db("rainbowDB", $con);
        			mysql_query("UPDATE areas SET content = '$area_a' WHERE area = 'area_a'");
        			header('Location: home.php');
        			mysql_close($con);
        		}
        	?>

 

The Showcase Image include file:

 

<?php
include_once 'admin-class.php';
$admin = new itg_admin();
$admin->_authenticate();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Admin Area</title>
        <script type="text/javascript" src="../tinymce/jscripts/tiny_mce/tiny_mce.js"></script>
        <script type="text/javascript">
        	tinyMCE.init({
        		mode : "textareas",
        		theme : "advanced",
        		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,fontselect,fontsizeselect",
        		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,cleanup,help,code,|,forecolor,backcolor",
        		theme_advanced_resizing : true
        	});
        </script>
    </head>
    <body>
        <p>
            Logged in as <?php echo $_SESSION['admin_login']; ?>.
        </p>
        <p>Showcase Image:</p>
	<?php include '../includes/image_upload_showcase.php'; ?>
	<?php
        	$con = mysql_connect("localhost","XXXXX","XXXXX");
        	if (!$con)
        	{
        		die('Could not connect: ' . mysql_error());
        	}
        	mysql_select_db("rainbowDB", $con);
        	mysql_query("UPDATE areas SET content = '$newname' WHERE area = 'showcase_image'");
        	mysql_close($con);
	?>
    </body>
</html>

 

The image_upload_showcase.php include file:

 

<?php
//define a maxim size for the uploaded images in Kb
define ("MAX_SIZE","1000"); 

//This function reads the extension of the file. It is used to determine if the
// file  is an image by checking the extension.
function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

//This variable is used as a flag. The value is initialized with 0 (meaning no 
// error  found)  
//and it will be changed to 1 if an errro occures.  
//If the error occures the file will not be uploaded.
$errors=0;
//checks if the form has been submitted
if(isset($_POST['update_showcase_image'])) 
{
	//reads the name of the file the user submitted for uploading
	$image=$_FILES['image']['name'];
	//if it is not empty
	if ($image) 
	{
	//get the original name of the file from the clients machine
		$filename = stripslashes($_FILES['image']['name']);
	//get the extension of the file in a lower case format
  		$extension = getExtension($filename);
		$extension = strtolower($extension);
	//if it is not a known extension, we will suppose it is an error and 
        // will not  upload the file,  
//otherwise we will do more tests
if (($extension != "jpg") && ($extension != "jpeg") && ($extension !=
"png") && ($extension != "gif")) 
		{
	//print error message
			echo '<h1>Unknown extension!</h1>';
			$errors=1;
		}
		else
		{
//get the size of the image in bytes
//$_FILES['image']['tmp_name'] is the temporary filename of the file
//in which the uploaded file was stored on the server
$size=filesize($_FILES['image']['tmp_name']);

//compare the size with the maxim size we defined and print error if bigger
if ($size > MAX_SIZE*1024)
{
echo '<h1>You have exceeded the size limit!</h1>';
$errors=1;
}

//we will give an unique name, for example the time in unix time format
$image_name=time().'.'.$extension;
//the new name will be containing the full path where will be stored (images 
//folder)
$newname="../../images/uploaded/".$image_name;
//we verify if the image has been uploaded, and print error instead
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied) 
{
echo '<h1>Copy unsuccessfull!</h1>';
$errors=1;
}}}}
?>
<form name="newad" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table class="tblInclude">
	<tr>
		<td><h4>Showcase Image:</h4></td>
	</tr>
	<tr>
		<td><input type="file" name="image"></td>
	</tr>
		<tr>
			<td><input name="update_showcase_image" type="submit" value="Upload Image"></td>
		</tr>
<?php
//If no errors registred, print the success message
if(isset($_POST['update_showcase_image']) && !$errors) 
{
	echo "<tr><td><img src=\"$newname\" width=\"220\" /></td></tr>";
}

?>
	</table>
</form>

 

The testimonials_text.php include file:

 

			<br />
		<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
			<table class="tblInclude">
        		<tr>
        			<td><h4>Testimonials Text:<h4></td>
        		</tr>
        		<tr>
        			<td><textarea name="testimonials_text">
        				<?php
        				$con = mysql_connect("localhost","XXXXX","XXXXX");
        				if (!$con)
        				{
        					die('Could not connect: ' . mysql_error());
      				  	}
        				mysql_select_db("rainbowDB", $con);
        				$result = mysql_query("SELECT * FROM areas WHERE area='testimonials_text'");
        				while($row = mysql_fetch_array($result))
        				{
        					echo $row['content'];
        				}
        				mysql_close($con);
        				?>
        			</textarea></td>
        		</tr>
        		<tr>
        			<td><input type="submit" value="Update" name="update_testimonials_text" /></td>
        		</tr>
        		</table>
        	</form>
        	<?php
        	if(isset($_POST['update_testimonials_text'])){
        		$testimonials_text = $_POST["testimonials_text"];
        		$con = mysql_connect("localhost","XXXXX","XXXXX");
       		 	if (!$con)
        		{
        			die('Could not connect: ' . mysql_error());
        		}
        		mysql_select_db("rainbowDB", $con);
        		mysql_query("UPDATE areas SET content = '$testimonials_text' WHERE area = 'testimonials_text'");
        		header('Location: home.php');
        		mysql_close($con);
        	}
        	?>

 

 

Thanks for any help!  ;D

Link to comment
https://forums.phpfreaks.com/topic/266269-problems-with-my-first-admin-area/
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.