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."; } } } ?>