josephbupe Posted January 21, 2013 Share Posted January 21, 2013 Hi, I have a code am trying to modify in order to allow writing more than one file to a MySQL database. Initially, the code was able to write one file into the database along with other information and saving the actual file into a directory. But after adding extra lines of code this isn't possible any more. I am not getting any error message. Kindly assist. Please, see attached file upload.php file. upload.php Quote Link to comment https://forums.phpfreaks.com/topic/273417-php-code-cant-write-more-than-one-file-to-mysql-database/ Share on other sites More sharing options...
Muddy_Funster Posted January 21, 2013 Share Posted January 21, 2013 please post up your actual code. people on this site (myself included) generaly avoid clicking on links that others whome we do not know post up. oh, and make sure you use the code tags to enclose the code that you post, to make everyones life easier. Quote Link to comment https://forums.phpfreaks.com/topic/273417-php-code-cant-write-more-than-one-file-to-mysql-database/#findComment-1407219 Share on other sites More sharing options...
josephbupe Posted January 21, 2013 Author Share Posted January 21, 2013 (edited) Thank you muddy_funster for your early response. Here is my upload.php code highlighting additions i made to uploading a second image (image2) and third image (image3) for the same record: <?php // Start a session for error reporting session_start(); // Call our connection file require("includes/conn.php"); // Check to see if the type of file uploaded is a valid image type function is_valid_type($file) { // This is an array that holds all the valid image MIME types $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif"); $valid_types = array("image2/jpg", "image2/jpeg", "image2/bmp", "image2/gif"); $valid_types = array("image3/jpg", "image3/jpeg", "image3/bmp", "image3/gif"); if (in_array($file['type'], $valid_types)) return 1; return 0; } // Just a short function that prints out the contents of an array in a manner that's easy to read // I used this function during debugging but it serves no purpose at run time for this example function showContents($array) { echo "<pre>"; print_r($array); echo "</pre>"; } // Set some constants // This variable is the path to the image folder where all the images are going to be stored // Note that there is a trailing forward slash $TARGET_PATH = "images/"; // Get our POSTed variables $ctitle = $_POST['ctitle']; $csubject = $_POST['csubject']; $creference = $_POST['creference']; $cyear = $_POST['cyear']; $cwidth = $_POST['cwidth']; $cheight = $_POST['cheight']; $cperiod = $_POST['cperiod']; $cdescription = $_POST['cdescription']; $image = $_FILES['image']; $image2 = $_FILES['image2']; $image3 = $_FILES['image3']; // Sanitize our inputs $ctitle = mysql_real_escape_string($ctitle); $csubject= mysql_real_escape_string($csubject); $creference = mysql_real_escape_string($creference); $cyear = mysql_real_escape_string($cyear); $cwidth = mysql_real_escape_string($cwidth); $cheight = mysql_real_escape_string($cheight); $cperiod = mysql_real_escape_string($cperiod); $cdescription = mysql_real_escape_string($cdescription); $image['name'] = mysql_real_escape_string($image['name']); $image2['name2'] = mysql_real_escape_string($image2['name2']); $image3['name3'] = mysql_real_escape_string($image3['name3']); // Build our target path full string. This is where the file will be moved do // i.e. images/picture.jpg $TARGET_PATH .= $image['name']; $TARGET_PATH .= $image2['name2']; $TARGET_PATH .= $image3['name3']; // Make sure all the fields from the form have inputs if ( $ctitle == "" || $csubject == "" || $creference == "" || $cyear == "" || $cwidth == "" || $cheight == "" || $cperiod == "" || $cdescription == "" || $image['name'] == "" || $image2['name2'] == "" || $image3['name3'] == "" ) { $_SESSION['error'] = "All fields are required"; header("Location: index.php"); exit; } // Check to make sure that our file is actually an image // You check the file type instead of the extension because the extension can easily be faked if (!is_valid_type($image)) { $_SESSION['error'] = "You must upload a jpeg, gif, or bmp"; header("Location: insert.php"); exit; } // Here we check to see if a file with that name already exists // You could get past filename problems by appending a timestamp to the filename and then continuing if (file_exists($TARGET_PATH)) { $_SESSION['error'] = "A file with that name already exists"; header("Location: insert.php"); exit; } // Lets attempt to move the file from its temporary directory to its new home if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) { // NOTE: This is where a lot of people make mistakes. // We are *not* putting the image into the database; we are putting a reference to the file's location on the server $sql = "insert into collections (ctitle, csubject, creference, cyear, cwidth, cheight, cperiod, cdescription, cfilename, cfilename2, cfilename3) values ('$ctitle', '$csubject', '$creference', '$cyear', '$cwidth', '$cheight', '$cperiod', '$cdescription', '" . $image['name'] . "', '" . $image2['name2'] . "', '" . $image3['name3'] . "')"; $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); header("Location: index.php"); exit; } else { // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to // Make sure you chmod the directory to be writeable $_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory"; header("Location: index.php"); exit; } ?> And also part of my insert.php file: <tr> <td>Upload Image1</td><td></td> <td> <input type="file" name="image" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> </tr> <tr> </td> <td>Upload Image2</td><td></td> <td> <input type="file" name="[color=#0000ff]image2[/color]" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> </tr> <tr> </td> <td>Upload Image3</td><td></td> <td> <input type="file" name="[color=#0000ff]image3[/color]" /><br /> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> <input type="submit" id="submit" value="Save" /> </td> </tr> I will appreciate your help. joseph Edited January 21, 2013 by josephbupe Quote Link to comment https://forums.phpfreaks.com/topic/273417-php-code-cant-write-more-than-one-file-to-mysql-database/#findComment-1407353 Share on other sites More sharing options...
josephbupe Posted January 25, 2013 Author Share Posted January 25, 2013 Please,does anyone know where I am going wrong with this code to upload three images to a same record?? Quote Link to comment https://forums.phpfreaks.com/topic/273417-php-code-cant-write-more-than-one-file-to-mysql-database/#findComment-1408108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.