Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6057597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:30:22+00:00 2026-05-23T08:30:22+00:00

I have been struggling for days to get a working upload onto the admin

  • 0

I have been struggling for days to get a working upload onto the admin section of my site and am almost there…!
I have a table in my database called test with 4 fields – id (int), title (varchar), desc (varchar) and photo (varchar) – the photo field represents the source of the image on the server.
My code is:

<?php include 'dbc.php'; page_protect();

if(!checkAdmin()) {header("Location: login.php");
exit();
}

$host  = $_SERVER['HTTP_HOST'];
$host_upper = strtoupper($host);
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
$path   = rtrim($login_path, '/\\');

foreach($_GET as $key => $value) {
    $get[$key] = filter($value);
}

foreach($_POST as $key => $value) {
    $post[$key] = filter($value);
}   
?>

<?php 

$target = "images/test/"; 
$target = $target . basename( $_FILES['photo']['name']); 

$title = mysql_real_escape_string($_POST['title']); 
$desc = mysql_real_escape_string($_POST['desc']);  
$pic = "images/test/" .(mysql_real_escape_string($_FILES['photo']['name']));

mysql_query("INSERT INTO `test` (`title`, `desc`, `photo`) VALUES ('$title', '$desc', '$pic')") ;

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
 } 
 else { 
 echo "Sorry, there was a problem uploading your file."; 
 } 
 ?> 

<form enctype="multipart/form-data" action="uploader.php" method="POST"> 
Title: <input type="text" name="title"><br> 
Description: <input type="text" name = "desc"><br>  
Photo: <input type="file" name="photo"><br> 
<input type="submit" value="Add"> 
</form>

For some reason when the row is entered into mysql a duplicate empty row is inserted so the table looks like:

ID              Title                Desc                   Photo 
15                                                          images/test/
16              test title           test description       images/test/test1.jpg

Is there any reason this is happening from the code above – its fairly rudimentary but given the pain and struggle its taken to get this working I really cant face starting again!!!

Thanks in advance for any help.

JD

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:30:22+00:00Added an answer on May 23, 2026 at 8:30 am

    as far as I see, your database code is executing when form is first time loading, but no file uploaded yet. So yo’ll need to move your database related code inside

    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
    

    Whole code:

    <?php include 'dbc.php'; page_protect();
    
    if(!checkAdmin()) {header("Location: login.php");
    exit();
    }
    
    $host  = $_SERVER['HTTP_HOST'];
    $host_upper = strtoupper($host);
    $login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF']));
    $path   = rtrim($login_path, '/\\');
    
    foreach($_GET as $key => $value) {
        $get[$key] = filter($value);
    }
    
    foreach($_POST as $key => $value) {
        $post[$key] = filter($value);
    }   
    ?>
    
    <?php 
    if($_FILES['photo']) //check if we uploading a file
    {
        $target = "images/test/"; 
        $target = $target . basename( $_FILES['photo']['name']); 
    
        $title = mysql_real_escape_string($_POST['title']); 
        $desc = mysql_real_escape_string($_POST['desc']);  
        $pic = "images/test/" .(mysql_real_escape_string($_FILES['photo']['name']));
    if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
    {
        mysql_query("INSERT INTO `test` (`title`, `desc`, `photo`) VALUES ('$title', '$desc', '$pic')") ;     
    
        echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
    } 
    else 
    { 
        echo "Sorry, there was a problem uploading your file."; 
        var_dump($_FILES); //for debug purposes
    }
    } 
    ?> 
    
    <form enctype="multipart/form-data" action="uploader.php" method="POST"> 
    Title: <input type="text" name="title"><br> 
    Description: <input type="text" name = "desc"><br>  
    Photo: <input type="file" name="photo"><br> 
    <input type="submit" value="Add"> 
    </form>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been struggling for almost three days now on this task, and I
I have been struggling for days trying to get a simple ActiveX DLL to
I have been struggling with this all day. I have an html table in
I have been struggling with this problem for a few days now and I
I have been struggling with that for 4 days now. I have a very
I have been struggling with this for three days, I want to do a
For the last couple of days of my X-mas holidays, I have been struggling
I've been struggling with this for days and just can't get it. Is it
I've been struggling with the following problem for some days now. I have found
I have been struggling with this for days. This works, but does not pass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.