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 8699833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:02:39+00:00 2026-06-13T02:02:39+00:00

I have a page that inserts records into a table. what i am having

  • 0

I have a page that inserts records into a table. what i am having a little bit of trouble with, is if the user decides that he does not want to include a picture, how do i submit the form without errors. Or is there a way to set the file field to select a default image from a folder and set it as the file entry if there is no selection from the user?

I have been able to solve the problem. Below is the code:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ($_FILES['photo'] && $_FILES['photo']['error'] == 0) {

$set['photo'] = "'" . $pix . "'";

$image = @getimagesize($_FILES['photo']['tmp_name']);
$size = ($_FILES["photo"]["size"]<= 512000);
if($image && $size = TRUE)
$pix= $_FILES['photo']['name'];

$kaboom = explode(".", $pix);
$pixExt = end($kaboom);
$photo= rand()."_".time().".".$pixExt; 
$tmpName = $_FILES['photo']['tmp_name']; 

$target = "../events/";

$target = $target . $photo; 
}
move_uploaded_file($tmpName, $target); 


    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "pd")) {
      $insertSQL = sprintf("INSERT INTO events (title, detail, `date`, photo) VALUES (%s, %s, %s, '$photo')",
                           GetSQLValueString($_POST['title'], "text"),
                           GetSQLValueString($_POST['detail'], "text"),
                           GetSQLValueString($_POST['date'], "text"));

      mysql_select_db($database_connMain, $connMain);
      $Result1 = mysql_query($insertSQL, $connMain) or die(mysql_error());

      $insertGoTo = "confirm.php?user=" . $row_rsadmin['aID'] . "";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
  • 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-06-13T02:02:40+00:00Added an answer on June 13, 2026 at 2:02 am

    If the user is allowed to store the data without sendign a picture, then it is not a very good idea to do the recording only if the file upload was successful.

    You should restructure the code, so that the image upload only happens if there is an image uploaded, so all the code that involves $_FILES['photo'] should be inside a block as such:

    if ($_FILES['photo'] && $_FILES['photo']['error'] == 0) {
        // upload file here
    }
    

    Also it is important to handle the database functions outside of this block, so you have to create a the record set outside of the file upload block, and add the image related values inside.

    // [...]
    $set = array(
        'title' => "'" . GetSQLValueString($_POST['title'], "text") . "'",
        // and so on ...
        'photo' => "null"
    );
    if ($_FILES['photo'] &&
        $_FILES['photo']['error'] == 0 &&
        $_FILES['photo']['size'] <= 512000) {
        $kaboom = explode(".", $pix);
        $photo = rand() . "_" . time() . "." . end($kaboom);
        $target = "../events/" . $photo;
        if (move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
            $set['photo'] = "'" . $photo . "'";
        }
    }
    // [...]
    $insertSQL = sprintf("INSERT INTO events (`%s`) VALUES (%s)",
        implode("`,`", array_keys($set)),
        implode(",", $set));
    // [...]
    

    This way you handle the errors if the user doesn’t upload a picture, and also make it possible to record the data without it.

    Edit: I have modified the above code to reflect the need that the photo field is always present in the insert statement.

    Edit 2: I fully implemented the file upload block in this edit, to get closer to the askers actual situation, also keeping it universal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page that builds out a table. Nothing else on that page
I have a page that lets the user edit the content of the page
On my page I have a form that inserts a new record in to
I have a button within my web page to inserts a few values into
I have a Table A with thousands of records. On each displayed page, only
I have a script im writing that inserts a record into my DB using
I have a page that uses the Telerik RadListView control and a Telerik RadDataPager
I have a page that I want to update non stop, every few seconds.
I have a page that consists of a header div for navigation, a content
I have a page that I am using an Ajax Accordion and when I

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.