Jump to content

Insert query problem


kurt.santo

Recommended Posts

The following worked well until I created a field file_name in table. I amended the insert query to insert the value of $filename into that field, but now it brings my error message without inserting into db/moving the file. The code is:

	for ($i = 0; $i < $counter; $i++) { // Handle each uploaded file.		// Create index names to refer to the proper upload and description.		$filename = 'upload' . $i;		// Check for a file.		if (isset($_FILES[$filename]) && ($_FILES[$filename]['error'] != 4)) {			// Add the record to the database.			$query = "INSERT INTO uploads (file_name, orig_file_name, file_size, file_type, user_id) VALUES ($filename, '{$_FILES[$filename]['name']}', {$_FILES[$filename]['size']}, '{$_FILES[$filename]['type']}', $user_id)";			$result = mysqli_query ($dbc, $query);

I have now the input fields as <p>File 1: <input type="file" name="upload1" /></p>(1 to 8)Is there a better way to get the $filename for each input field?Kurt

Link to comment
Share on other sites

What is the PHP/SQL error message?
It just prints my else clause "sorry etc" message (else clause if query did not run ok). Doing a var_dump() on query it shows:string(129) "INSERT INTO uploads (file_name, orig_file_name, file_size, file_type, user_id) VALUES (upload3, '3-24.gif', 964, 'image/gif', 67)"Cannot see what is wrong with that. Can you?Kurt
Link to comment
Share on other sites

upload3 doesn't have quotes around it. You can also run it on phpMyAdmin to see an error.
Thanx, does the trick now, but came across a different issue. The file_name is now actually a combination of user_id and number of file upload field as such. When I upload with same input field again it creates a new record. We want to allow for later changes of image data and you already said it can be done with a column b as such, so that once image is approved it can be copied over into column a. I have two problems in how to do that:1. How do I deal with the other info as image size etc? would I have a column b for those columns as well?2. How do I prevent that another record gets inserted with same file_name? There should really only one as I want to display those image on web page. Also, the first upload as such needs to be approved as a whole. So when a user changes his/her mind it would make sense that he/she can upload another photo with same name, but in this case it should replace the other uploaded one. Once all necessary data is uploaded it can be approved and once done, user can change individul files (this time wiht the copying from column b into column a)...Kurt
Link to comment
Share on other sites

You shouldn't need to store the file size, you can always read the file to get that kind of information. All you should need is the file name. If you want to make it so that files don't overwrite each other then you'll need to check to see if the file exists and rename the new file if it does, before you move it. I posted a loop to do that in another thread.

Link to comment
Share on other sites

You shouldn't need to store the file size, you can always read the file to get that kind of information. All you should need is the file name. If you want to make it so that files don't overwrite each other then you'll need to check to see if the file exists and rename the new file if it does, before you move it. I posted a loop to do that in another thread.
Would I then only need a field with the image name and user_id in table? Do I not also need to keep original name? If not, then I could actually just have table with fields image1, image2, image3, image4 (and some more) plus other fields for a description, a url etc. Would that be ok?Kurt
Link to comment
Share on other sites

The original name doesn't matter, the only thing you need to store is the filename to point to the file on the server. It doesn't matter what it was originally called. You could add 8 image fields to the user table if you want, it would only be necessary to have a separate table that connects users and images if a user could have as many images as they want. I've added a few other fields to the users table and another table for paypal logs if you're wondering what those are.

Link to comment
Share on other sites

The original name doesn't matter, the only thing you need to store is the filename to point to the file on the server. It doesn't matter what it was originally called. You could add 8 image fields to the user table if you want, it would only be necessary to have a separate table that connects users and images if a user could have as many images as they want. I've added a few other fields to the users table and another table for paypal logs if you're wondering what those are.
Just one more question before I start trying:As I want to have main images and thumbs (8 for each) and I learned from my other thread that I should resize upon file upload. Would I need then 8 fields in table for file names of main images, 8 fields for names of thumbs (the same, just stored in different folder) plus the other fields for user_id, description, url and so on? Also, as I want later on the user to be able to change files, which need to be quality checked and should only be copied into main columns if they pass validation: Then I would also need for all image columns a column b, wouldn't I? I does not matter that there are quite a few fields, does it? There is no restriction in how many columns you can have per table?Kurt
Link to comment
Share on other sites

There might be a restriction on how many columns, but if there is it's something like 65,535 or something large. If the thumbnails have the same name then you probably don't need another column to store just those. If you have an image and you renamed the image to "image_xyz_1.jpg", then you can just save that in the database. It's best to save only the filename, not the folder path. That way you can change the folder without needing to update the database. Have a variable somewhere that stores the base path to the images, e.g. $IMAGE_BASE_PATH = "http://domain.co.uk/user/images/"; and $THUMB_BASE_PATH = "http://domain.co.uk/user/images/thumbs/"; Then you would just tack on the filename to the end of whatever path you want to use when you display it. For approval, you do need a second column for each. The act of approving would involve deleting the file in column 1 from the server, copying the name in column 2 to column 1, and clearing column 2. Disapproval would mean deleting the file in column 2 and then clearing column 2.

Link to comment
Share on other sites

There might be a restriction on how many columns, but if there is it's something like 65,535 or something large. If the thumbnails have the same name then you probably don't need another column to store just those. If you have an image and you renamed the image to "image_xyz_1.jpg", then you can just save that in the database. It's best to save only the filename, not the folder path. That way you can change the folder without needing to update the database. Have a variable somewhere that stores the base path to the images, e.g. $IMAGE_BASE_PATH = "http://domain.co.uk/user/images/"; and $THUMB_BASE_PATH = "http://domain.co.uk/user/images/thumbs/"; Then you would just tack on the filename to the end of whatever path you want to use when you display it. For approval, you do need a second column for each. The act of approving would involve deleting the file in column 1 from the server, copying the name in column 2 to column 1, and clearing column 2. Disapproval would mean deleting the file in column 2 and then clearing column 2.
Mercy me, what did I get myself into;-) So much different things to do... Will create a new table and see how far I get. I found the following code, which should do what I after for the thumbs. Guess I just have then to copy this function, to rename the function and change the sizes to apply to main images. The function I found on webpage you gave me is (and works well when naming the appropriate file in code - will be more difficult to figure out how to do it for the uploaded files):
$filename = 'b.jpg';// Set a maximum height and width$width = 90;$height = 90;// Content typeheader('Content-type: image/jpeg');// Get new dimensionslist($width_orig, $height_orig) = getimagesize($filename);$ratio_orig = $width_orig/$height_orig;if ($width/$height > $ratio_orig) {   $width = $height*$ratio_orig;} else {   $height = $width/$ratio_orig;}// Resample$image_p = imagecreatetruecolor($width, $height);$image = imagecreatefromjpeg($filename);imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);// Outputimagejpeg($image_p, null, 100);

Am a bit worried as it only allows for jpgs. Is there a way to check in this function what file type it is? I saw examples were you can create long arrays of possible image types. Would it not be an idea to store the image type in db and store in variable? Or would you somehow incorporate the getimagesize function?Kurt

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...