upload multiple file paths into one MYSQL record

I have a simple project, so I thought, that involves uploading multiple images to a server folder as well as adding the file paths of the uploaded images to a single MySQL record. Below is the table I set up as well as the PHP/HTML I am using. It’s code I rehashed from another single file upload. That one works, but this one does nothing. In any case I need to upload three file paths on this database and then I have to make one that can handle at least 10 on the next. I only do some light javascripting, so I am not sure where I am going wrong in PHP.

Please help! Thanks

Ivan Rios

*****MySQL Table[/b]

CREATE TABLE photos (
id int(11) NOT NULL,
mainphoto varchar(100) NOT NULL,
description varchar(100) NOT NULL,
photo1 varchar(100) NOT NULL,
caption1 varchar(100) NOT NULL,
photo2 varchar(100) NOT NULL,
caption2 varchar(100) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

*****HTML Form:

Main Photo:

Description

Photo 1:

Caption 1

Photo 2:

Caption 2

*****This is my PHP code: uploadaction.php

<?php include('Connections/postcardPhotos.php'); ?> <?php define ('MAX_FILE_SIZE', 1024 * 50000000); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "uploads")) { // make sure it's a genuine file upload if (is_uploaded_file($_FILES['image']['tmp_name'])) { // replace any spaces in original filename with underscores $filename = str_replace(' ', '_', $_FILES['image']['name']); // get the MIME type $mimetype = $_FILES['image']['type']; if ($mimetype == 'image/pjpeg') { $mimetype= 'image/jpeg'; } // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/png', 'image/jpg', 'image/bmp', 'image/tif', 'image/pdf'); // upload if file is OK if (in_array($mimetype, $permitted) && $_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) { switch ($_FILES['image']['error']) { case 0: // get the file contents $image = file_get_contents($_FILES['image']['tmp_name']); // get the width and height $size = getimagesize($_FILES['image']['tmp_name']); $width = $size[0]; $height = $size[1]; //This is the directory where images will be saved $target = "uploads/"; $target = $target . basename( $_FILES['image']['name']); //Writes the photo to the server if(move_uploaded_file($_FILES['image']['tmp_name'], $target)); { } $insertSQL = sprintf("INSERT INTO photos (mainphoto, `description`, photo1, caption1, photo2, caption2) VALUES (%s, %s, %s, %s, %s, %s)", GetSQLValueString($filename['mainphoto'], "text"), GetSQLValueString($_POST['description'], "text"), GetSQLValueString($filename['photo1'], "text"), GetSQLValueString($_POST['caption1'], "text"), GetSQLValueString($filename['photo2'], "text"), GetSQLValueString($_POST['caption2'], "text")); mysql_select_db($database_postcardPhotos, $postcardPhotos); $Result1 = mysql_query($insertSQL, $postcardPhotos) or die(mysql_error()); if ($Result1) { $result = "$filename uploaded successfully."; } else { $result = "Error uploading $filename. Please try again."; } break; case 3: case 6: case 7: case 8: $result = "Error uploading $filename. Please try again."; break; case 4: $result = "You didn't select a file to be uploaded."; } } else { $result = "$filename is either too big or not an image."; } } } ?>

First thing to do is restructure your datatable design. You should have a single filepath, image (record) per row and not combine them into a single record.

I need to recall the three photos for use on a web page that will display the three photos as a per record view. Can I do that with loading a common variable in each record like a job number or similar ID?

That is how it is typically done.

House table
[php]
tbl_Houses
id
name
address[/php]

Image table
[php]
tbl_Images
id
imageLocation[/php]

Linking table
[php]tbl_Houses_Images
id
houseID
imageID[/php]

I am getting an EOF message and I can’t figure it out. Also I think I need to set up an image list array at the end I think line 71. Not sure about that either.

[php]

<?php require_once('Connections/postcardsDB.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } mysql_select_db($database_postcardsDB, $postcardsDB); $query_imagelist = "SELECT upload_data.property_ID, upload_data.FILE_NAME FROM upload_data"; $imagelist = mysql_query($query_imagelist, $postcardsDB) or die(mysql_error()); $row_imagelist = mysql_fetch_assoc($imagelist); $totalRows_imagelist = mysql_num_rows($imagelist); mysql_select_db($database_postcardsDB, $postcardsDB); $query_listing = "SELECT listings.property_ID, listings.address1, listings.address2, listings.county, listings.price, listings.`description` FROM listings"; $listing = mysql_query($query_listing, $postcardsDB) or die(mysql_error()); $row_listing = mysql_fetch_assoc($listing); $totalRows_listing = mysql_num_rows($listing); mysql_select_db($database_postcardsDB, $postcardsDB); $query_ShowPics = "SELECT `listings`.`property_ID`, `upload_data`.`FILE_NAME` FROM `listings`, `upload_data` ORDER BY `listings`.`property_ID` DESC, `upload_data`.`FILE_NAME` DESC"; $ShowPics = mysql_query($query_ShowPics, $postcardsDB) or die(mysql_error()); $row_ShowPics = mysql_fetch_assoc($ShowPics); $totalRows_ShowPics = mysql_num_rows($ShowPics); mysql_free_result($imagelist); mysql_free_result($listing); mysql_free_result($ShowPics); ?>

<!doctype html>

Untitled Document

<?php echo $row_ShowPics['property_ID']; ?>

<?php do { ?>

<?php echo $row_ShowPics['FILE_NAME']; ?>

<?php while ($row_imagelist = mysql_fetch_assoc($imagelist)); ?>

[/php]

On line 69 you start a do while loop. You have an opening bracket, but no closing bracket, unless I missed it.

You could setup an array to hold the list, it kind of depends on how you are going to use the images and when.

Sponsor our Newsletter | Privacy Policy | Terms of Service