Problems With My First Admin Area

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]<?php
include_once ‘…/admin-class.php’;
$admin = new itg_admin();
$admin->_authenticate();
?>

Admin Area

Logged in as <?php echo $_SESSION['admin_login']; ?>.


HOME PAGE


<?php include 'includes/area_a.php'; ?> <?php include 'includes/showcase_image.php'; ?> <?php include 'includes/testimonials_text.php'; ?>


[/php]

This is the include file for Area A:

[php]











Area A:



<?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);
?>



<?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);
}
?>[/php]

The Showcase Image include file:

[php]<?php
include_once ‘admin-class.php’;
$admin = new itg_admin();
$admin->_authenticate();
?>

Admin Area

Logged in as <?php echo $_SESSION['admin_login']; ?>.

Showcase Image:

<?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); ?> [/php]

The image_upload_showcase.php include file:

[php]<?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 ‘

Unknown extension!

’;
$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 ‘

You have exceeded the size limit!

’;
$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 ‘

Copy unsuccessfull!

’;
$errors=1;
}}}}
?> <?php //If no errors registred, print the success message if(isset($_POST['update_showcase_image']) && !$errors) { echo ""; }

?>

Showcase Image:

[/php]

The testimonials_text.php include file:

[php]












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);
?>


<?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);
}
?>[/php]

Thanks for any help! ;D

Do you mean that the file does not stay? Normally, you do not use the “copy” function to copy the temporary uploaded file. Instead you use the “move_uploaded_file” function. Here is the info about it:
http://php.net/manual/en/function.move-uploaded-file.php

Or, do you mean that the database is not keeping the data?

Sponsor our Newsletter | Privacy Policy | Terms of Service