I hope you guys can help me. I would like to store multiple images into mysql along with some data using a html form.
To be more specific, I’d like to store images (for example 5 or 7 ect.) assigned to a Post ID.
Something like this.
I’ve written a basic script to insert text to database but text only and now I got stuck.
It probably is possible to combine the two scripts but I wouldn’t suggest it’s a good idea. Taking two disparate scripts and trying to combine them is seldom a practical idea. Better would be to start with one script and then begin adding the features of the other - note I say the features, not the code. Of course, this means first understanding how each of the scripts works.
The job of uploading files to the server or database can be done by PHP alone without the need for JS/jQuery.
You are right. Is there any tutorial you can show me? I’ve searched the web but didn’t find any good one. To be honest I just got into PHP so it would be nice.
I’m not suggesting it’s the best but a quick Google for how to upload a file in php brought up this tutorial which you should be able to incorporate reasonably easily into the script you posted.
You might want to consider whether you really want to store the images in your database or take the WordPress approach and simply store them in an uploaded files directory on the server.
Depending on how many pictures you’re allowing folks to upload, you could either have multiple input fields, naming them user_image1, user_image2 etc or use JS to add new fields as required (they would still be named user_image1, user_image2 etc).
However, get the script working with one image first before you attempt to make any changes to it. That way, you have more chance of finding where you’ve gone wrong (assuming you do go wrong at some point ).
One other thing, once you have it working, do change the form so it no longer uses tables - that is a truly awful way to format tables!
The script is working fine, I tested it.
Yeah, I was thinking it, I mean to create for example 5 new fields. But lets just assume that some user want to upload more than 5 pictures. JS could do it as I saw it in some examples (adding multiple fields/images) with an “Add more” button but I have no idea how to start
I suppose you would first modify your php script to be able to take an undetermined number of uploads. Possibly by creating an array: <input name="upload[]"
Then ask in the js forum about adding more inputs.
In the processing for this, the obvious choice would be a foreach loop to run the upload script on the array. But you would be wise to set a maximum number of uploads, so perhaps a while or for would be better.
$max = 5; // Maximum number of uploads allowed
$n = 0;
while($n < $max) {
// Process Upload for $_FILES['upload'][$n]
$n++; // Up one
}
Yes, since we don’t know how many, we can just loop through it.
I think the first thing in the loop should be “if is set”, as fewer than the max number would confuse things.
while($n < $max) {
if(isset($_FILES['upload'][$n])) {
// rest of process
}
$n++; // Up one
}