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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:22:10+00:00 2026-05-27T07:22:10+00:00

I have several forms, with different fields and purposes. So i tought hey, i’ll

  • 0

I have several forms, with different fields and purposes.

So i tought “hey, i’ll build a generic insert.php page to input data to my database”.
Ok, so far so good.

But now i need to link files to each record, so my form has a <input type="file"> field.
What i really want to do is to allow the user to select several files (pictures, videos), create a directory named like the table plus the id of the record that is being inserted (upload/$tablename/$id/), store the files and add the path to the database.

Something like:

ID | NAME | WEBPAGE  | FILES  
01 | john | john.com | /upload/tblWebpages/01/  
02 | mike | mike.net | /upload/tblWebpages/02/  
19 | jimy | jimy.org | /upload/tblWebpages/19/

Because i don’t know how many files will be inserted or their names, and later i intend to fetch all images in that directory and display it.

Is it possible to know the record ID from the table at the time the upload is being processed?
Because, as explained above, i’d like to store the files under a directory called $ID.
How should i add it to my query?

Alright, to the code:

insert.php

include('classes/class.upload.php');

$server = "localhost";
$user = "root";
$password = "xxxxx";
$database = "myDB";
$tabela = $_POST['tabela'];
$diretorio = "upload/".$tabela."/";

mysql_connect($server,$user,$password);
mysql_select_db($database) or die("Unable to select database" . mysql_error());

// create an array to hold your filnames
$images = array();
$files = array();
foreach ($_FILES['imagem'] as $k => $l) {
    foreach ($l as $i => $v) {
        if (!array_key_exists($i, $files))
        $files[$i] = array();
        $files[$i][$k] = $v;
    }
}      
foreach ($files as $file) {
    $handle = new Upload($file);
    if ($handle->uploaded) {
        $handle->Process($diretorio);
        if ($handle->processed) {
            echo 'OK ';
            // add the filename to the array
            $images[] = $handle->file_dst_pathname;
        } 
        else { echo 'Upload error: ' . $handle->error; }
    } 
    else { echo 'Upload error: ' . $handle->error; }
unset($handle);

}

foreach ($_POST as $field => $value) {
    if (!preg_match("/$value/i", $tabela)) {
        if (preg_match("/$field/i", 'data')) {$value = date('Y-m-d', strtotime($value));}
        else {$value = mysql_real_escape_string($value);}
        $campo .= $field . ',';
        $valor .= '\'' . $value . '\'' . ',';
        $query = "INSERT INTO $tabela ($campo) VALUES ($valor)";
        $query = str_replace(",)",")",$query);
        //echo " inside foreach  RESULT = $result  and  QUERY = $query  <br>";
    }
}

$result = mysql_query($query);
echo " outside foreach  RESULT = $result  and  QUERY = $query  <br>";

if (!$result) {
    $message  = 'Invalid query: ' . mysql_error() . "\n";
    $message .= 'Whole query: ' . $query;
    die($message);
}

mysql_close();

form.php (simplified)

<form method="POST" action="insert.php" class="classeform" enctype="multipart/form-data">
  <label for="namee" class="lbl-d-input">Name: </label>
  <input type="text" name="namee" id="namee" class="field">

  <label for="webpage" class="lbl-d-input">Webpage: </label>
  <input type="text" name="webpage" id="webpage" class="field">

  <label for="files" class="lbl-d-input">Files: </label>
  <input type="file" name="files[]" id="files" class="field-img">

  <input type="hidden" name="tabela" value="mytbl">

  <input type="submit" value="Enviar" class="btn-enviar">
</form>
  • 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-27T07:22:10+00:00Added an answer on May 27, 2026 at 7:22 am

    Got it to work with mysql_insert_id()

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

Sidebar

Related Questions

I have several forms on one page, they're all the same, but have different
I have a windows forms app where I have split different functionality into several
I want to have a feature to submit several forms on a page when
I have a page that has several ajax submission forms. Each form has a
I am using the profile module and have several categories for different fields. I
I have a search form that allows users to search on several different fields
I have a page with several forms on one page. I have a button
I have a page with several forms. I am trying to submit one of
I have several stages to an application process consisting of multiple forms on different
I have several forms like this: <table width=100% border=0 cellpadding=0 cellspacing=0 class=table_std> <tr id=exam_String_newValue_row>

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.