Inserting single or pre-defined number of image(s)/file(s) or its description into MySQL database is no problem for me but undefined number of it, which will be added the input fields by users voluntarily, in single submit is difficult for me. Anyone has the solution ?
2 2223
I’m not really seeing the problem: - <form action="process.php" method="post">
-
<input type="file" name="upload[]">
-
<input type="file" name="upload[]">
-
…
-
<input type="file" name="upload[]">
-
<button type="submit">upload ’em all</button>
-
</form>
- foreach ($_FILES["upload"] as $file)
-
{
-
// insert into DB
-
}
@Dormilich
First of all I thank you for your reply, Dormilich. I have tried it once before but only the last item went into the database. I think the problem is how to handle the array well in php.
I'm currently developing a website. The related page URL is http://www.ehyeh.com/validatedformtest3.html.
The following is the complete form page code : - <!DOCTYPE html>
-
-
<html>
-
<head>
-
<title>Submit Form</title>
-
-
<!-- include the Tools -->
-
<script src="/jquery.tools.min.js"></script>
-
-
<!-- same styling as in minimal setup -->
-
<link rel="stylesheet" type="text/css" href="/form.css"/>
-
-
<!-- override it to have a columned layout -->
-
<link rel="stylesheet" type="text/css" href="/columns.css"/>
-
-
<script language="javascript">
-
<!--
-
function OpenWindow(url,intWidth,intHeight) {
-
window.open(url, "_blank", "width="+intWidth+",height="+intHeight+",resizable=0,scrollbars=0") ;
-
}
-
//-->
-
</script>
-
-
<script language="javascript">
-
<!--
-
fields = 0;
-
images = new Array();
-
image_title = new Array();
-
-
function addInput() {
-
if (fields != 3) {
-
var htmlText = "<input type='file' name='images' id='fields' value='' ACCEPT='filetype/*' />";
-
var deleteField = "<a style='cursor:pointer;color:yellow;' onclick='this.parentNode.parentNode.removeChild(this.parentNode), fields -=1;'> X</a><br />"
-
var imageTitle = "Title of the photo/image if any<input type='text' name='image_title' id='fields' value='' size='35' /><br /><br />";
-
var stringConcatenated = htmlText + deleteField + imageTitle;
-
var newElement = document.createElement('div');
-
newElement.id = ''; // Give the DIV an ID, if you need to reference it later...
-
newElement.innerHTML = stringConcatenated;
-
-
var fieldsArea = document.getElementById('files_list');
-
fieldsArea.appendChild(newElement);
-
-
fields += 1;
-
} else {
-
alert("Only 3 upload fields allowed.");
-
document.form.add.disabled=true;
-
}
-
}
-
//-->
-
</script>
-
-
<style type="text/css">
-
a {font-family : Arial; Font-size : 12px; text-decoration : none}
-
a:link {color : pink}
-
a:visited {color : pink}
-
a:active {color : blue}
-
a:hover {color : #3399FF}
-
</style>
-
-
</head>
-
-
<body background="bg1.gif">
-
<table width="500px" align="center">
-
<tr><td bgcolor="#333"> <!-- I tested with '<tr width="500px" align="center">' eliminating table tags, but the result was same as before -->
-
<form action="process-form-data-post.php" method="post" enctype="multipart/form-data" name="wordcount" id="myform" class="cols">
-
<script language="JavaScript">
-
function countit(){
-
var formcontent=document.wordcount.message.value
-
formcontent=formcontent.split(" ")
-
document.wordcount.wordcount3.value=formcontent.length
-
}
-
</script>
-
<h4>We strictly don't use the input data for other purposes.</h4>
-
-
<fieldset><font=+2>
-
<label> First name * </label><input type="text" name="first_name" id="first_name" required="required" maxlength="25" />
-
<label> Last name * </label><input type="text" name="last_name" id="last_name" required="required" maxlength="25" />
-
</fieldset>
-
<fieldset>
-
<label> E-mail address * </label><input type="email" name="e_list" id="e_list" required="required" maxlength="70" />
-
<label> Living City </label><input type="text" name="livingcity" id="livingcity" maxlength="25" />
-
</fieldset>
-
<fieldset>
-
<label> Message * Maxlength : 5000 words </label><input type="button" value="Click to Check Words Length" onClick="countit()"> <input type="text" name="wordcount3" size="5">
-
<textarea rows=15 cols=59 name="message" id="message" wrap="physical" required="required" maxlength="10000" />Under construction.</textarea> <p>
-
<input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
-
<input type="button" name="imagesubmit" onclick="addInput()" value="Attaching file/photo(s)" />
-
<div id="files_list"></div>
-
<script>
-
<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
-
var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 30 );
-
<!-- Pass in the file element -->
-
multi_selector.addElement( document.getElementById( 'my_file_element' ) );
-
</script>
-
</font>
-
</fieldset>
-
<fieldset>
-
<label> <a href="javascript:OpenWindow('spellcheck2.html','344','285')">SPELL CHECK</a></label>
-
</fieldset>
-
<div class="clear"></div>
-
<button type="submit" name="s1" id="s1">Submit form</button>
-
<button type="button" class="close" onclick="history.back();"> Cancel/Back </button>
-
</form>
-
</td></tr>
-
</table>
-
<script>
-
-
// Regular Expression to test whether the value is valid
-
$.tools.validator.fn("[type=time]", "Please supply a valid time", function(input, value) {
-
return /^\d\d:\d\d$/.test(value);
-
});
-
-
$.tools.validator.fn("[data-equals]", "Value not equal with the $1 field", function(input) {
-
var name = input.attr("data-equals"),
-
field = this.getInputs().filter("[name=" + name + "]");
-
return input.val() == field.val() ? true : [name];
-
});
-
-
$.tools.validator.fn("[minlength]", function(input, value) {
-
var min = input.attr("minlength");
-
-
return value.length >= min ? true : {
-
en: "Please provide at least " +min+ " character" + (min > 1 ? "s" : ""),
-
fi: "Kentän minimipituus on " +min+ " merkkiä"
-
};
-
});
-
-
$.tools.validator.localizeFn("[type=time]", {
-
en: 'Please supply a valid time',
-
fi: 'Virheellinen aika'
-
});
-
-
-
$("#myform").validator({
-
position: 'top left',
-
offset: [-12, 0],
-
message: '<div><em/></div>' // em element is the arrow
-
});
-
</script>
-
</body>
-
</html>
-
And the following is the complete processing form data php code : - <?php
-
// Connecting to the MySQL server
-
$db = mysql_connect("host", "username", "password");
-
if (!$db)
-
{
-
die('Could not connect: ' . mysql_error());
-
}
-
mysql_select_db("databasename",$db) or die(mysql_error());
-
-
// Inserting these values into the MySQL table
-
$_POST[message] = mysql_real_escape_string($_POST[message]);
-
$_POST[first_name] = mysql_real_escape_string($_POST[first_name]);
-
$_POST[last_name] = mysql_real_escape_string($_POST[last_name]);
-
$_POST[livingcity] = mysql_real_escape_string($_POST[livingcity]);
-
-
$uploadDir = '/directory/path/';
-
-
/*
-
if(count($_POST['images']))
-
{
-
$len = count($_POST['images']);
-
-
for ($i=0; $i < $len; $i++)
-
{
-
$images = $_POST['images'][$i];
-
$image_title = $_POST['image_title'][$i];
-
}
-
}
-
*/
-
-
if(isset($_POST['s1']))
-
{
-
if($images) {
-
$fileName = $_FILES['images']['name'];
-
$tmpName = $_FILES['images']['tmp_name'];
-
-
$filePath = $uploadDir . $fileName;
-
-
$result = move_uploaded_file($tmpName, $filePath);
-
if (!$result) {
-
echo "Error uploading file";
-
exit;
-
}
-
-
if(!get_magic_quotes_gpc())
-
{
-
$filePath = addslashes($filePath);
-
}
-
}
-
}
-
-
foreach ($filePath as $v1) {
-
// foreach ($v1 as $v2) {
-
$query = "INSERT INTO databasename (path) VALUES ('$filePath')";
-
}
-
//}
-
-
//$filePath = serialize($filePath); $filePath = implode(',',$filePath);
-
$image_title = mysql_real_escape_string($image_title); //$_POST[image_title]
-
$image_title = serialize($image_title);
-
//$image_title = implode(',',$image_title); 'Invalid arguments passed' error message was produced
-
//$image_title = join(" ",$image_title); also 'Invalid arguments passed' error message was produced
-
$query = "INSERT INTO databasename (first_name, last_name, e_list, livingcity, message, image_title, submit_date, path) VALUES ('$_POST[first_name]', '$_POST[last_name]', '$_POST[e_list]', '$_POST[livingcity]', '$_POST[message]', '$image_title', NOW(), '$filePath')";
-
$result = mysql_query($query,$db);
-
if (!$result){
-
die('Error: ' . mysql_error());
-
}
-
echo <<<_END
-
<html>
-
<head>
-
<title>Processing Form Data</title>
-
<!-- include the Tools -->
-
<script src="/jquery.tools.min.js"></script>
-
-
<!-- same styling as in minimal setup -->
-
<link rel="stylesheet" type="text/css" href="/form.css"/>
-
-
<!-- override it to have a columned layout -->
-
<link rel="stylesheet" type="text/css" href="/columns.css"/>
-
-
<style type="text/css">
-
a {font-family : Arial; Font-size : 12px; text-decoration : none}
-
a:link {color : pink}
-
a:visited {color : pink}
-
a:active {color : pink}
-
a:hover {color : #3399FF}
-
</style>
-
-
</head>
-
<body background="bg1.gif" link="white">
-
<table align="center">
-
<tr><td> </td></tr><tr><td> </td></tr>
-
</table>
-
<table id="myform" class="cols" align="center">
-
<tr><td> </td></tr>
-
<tr><td align="center">
-
<h2>Thank you for submitting your inputs !</h2>
-
</td></tr>
-
<tr><td> </td></tr>
-
<tr><td align="center"><font size="+1"><a href="http://www.ehyeh.com">Back to Home Page</a></font></td></tr><tr><td> </td></tr>
-
</table>
-
</body>
-
</html>
-
_END;
-
-
mysql_close($db);
-
?>
-
And the following is a screenshot of the database table. Only the last item which I input 'a' in the field of 'image_title' was inserted into the database.
I don't think it could be the problem of that the inconsistency of the use of the array in the javascript and the php script.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Spidah |
last post by:
Is there an easier way to get the number of items in a database table than
selecting all the records in it and then running mysql_num_rows on the
result?
Hamilton
www.laughland.biz
|
by: Jonas Meurer |
last post by:
hello,
i develop a project with a mysql interface. one mysql table holds all
the images for my project.
everything works quite well so far, except i'm not able to upload images
into the...
|
by: Dan Anderson |
last post by:
I have created a BLOB field to store images. Is there any way to embed
them within HTML with something like:
<image start: jpeg>
</image>
Thanks in advance,
Dan
|
by: Matt |
last post by:
Hi
I'm have difficulty displaying images that are stored in a MySQL database.
I've tried using the odbc.net provider, the ByteFX provider and the
MySQLDriverCS one but can't seem to get the...
|
by: marcin |
last post by:
I have problems with inserting Japanese chars into MySQL database. This
operation results in two completetely different strings that I can see
via phpMyAdmin:
$word: SELECT word FROM table;...
|
by: akashkhasgiwala |
last post by:
i am developing i web application in which every user will have an Xml File containing mostly text and date/time stamps
this application will use MySql for storing data
so pl tell me if i can STORE...
|
by: tolkienarda |
last post by:
hi all
i need to allow people to upload images to my site and then save them in a mysql database. i've looked at some prebuilt scripts using iframes but i don't think that will work for me. I want...
|
by: mannavavp |
last post by:
hi friends,
please let me know how i can store audio files in database table using java programming?
|
by: Keith Hughitt |
last post by:
Hi all,
I've run into a strange error while trying to store some PNG images in
a MySQL database using MySQLdb. When I try to insert smaller images (<
64kb or so) everything seems to work fine....
|
by: poopsy |
last post by:
hello all,
i am developing a web application for my final year project where i will be able to download and upload files(more specifically, it will be papers and articles, mostly pdf files) from the...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
| |