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>
Got it to work with
mysql_insert_id()