Jump to content

Insert the form values from an array into a database as one string


Mechaworx

Recommended Posts

Hi everyone,

 

I'm new to this group and new to php. I have created a multi-part form that allows the user the option to add multiple input fields to a form to upload images. Here is the form structure:

 

<form action="Scripts/processreports2.php" method="post" enctype="multipart/form-data" name="report_form" target="uploader" class="reportfrm">
<fieldset>
        <legend>Upload your images</legend>
        <ol id="add_images">
          <li>
        	<input type="file" class="input" name="files[]" />
          </li>
  <li>
        	<input type="file" class="input" name="files[]" />
          </li>
  <li>
        	<input type="file" class="input" name="files[]" />
          </li>
        </ol>
        <input type="button" name="addFile"  id="addFile" value="Add Another Image" onclick="window.addFile(this);"/>
</fieldset>
        <p>* indicates a required field.</p>
        <input name="submit" type="submit" id="submit" value="Send Info!" />
</form>

 

Through php a maximum of three input fields are fed into an array that checks to make sure that the uploaded files are images and not some other type of file. The uploading porition of this script works.

 

Now I am trying to get the values of the input fields as a string and insert them into the database as one record.

 

Let's say the user has three files they want to upload. I have managed to get the files as a string ie; file1.jpg, file2.jpg, file3.jpg but what is happening is that I am getting three separate records with  file1.jpg, file2.jpg, file3.jpg in them. If the user has only two files to upload then I get two separate records with file1.jpg and file2.jpg in them. (Hope that makes sense). I want one record.

 

I have been struggling with this since Monnday and while every day I get closer, this is as close as I can get. Below is the php code.

 


#connect to the database
mysql_connect("localhost", "root", "");
mysql_select_db("masscic");

//Upload Handler to check image types
function is_image($file) {

$file_types = array('jpeg', 'gif', 'bmp'); //acceptable file types

if ($img = getimagesize($file)){

	//echo '<pre>';
	//print_r($_FILES); used for testing
	//print_r($img); used for testing

	if(in_array(str_replace('image/', '', $img['mime']), $file_types))
	return $img;
}
return false;
}
//form submission handling
if(isset($_POST['submit'])) {

//file variables

$fname = $_FILES['files']['name'];
$ftype = $_FILES['files']['type'];
$fsize = $_FILES['files']['size'];
$tname = $_FILES['files']['tmp_name'];
$ferror = $_FILES['files']['error'];
$newDir = '../uploads/'; //relative to where this script file resides

for($i = 0; $i < count($fname); $i++) {
    //echo 'File name ' . $fname[$i] . ' has size ' . $fsize[$i]; used for testing
	if ($ferror[$i] =='UPLOAD ERR OK' || $ferror[$i] ==0) 
	{	
		if(is_image($tname[$i]))
		{
			//append the tmp_name($tname) to the file name ($fname) and upload to the server
			move_uploaded_file($tname[$i], ($newDir.time().$fname[$i])); 
			echo '<li><span class="success">'.$fname[$i].' -- image has been accepted<br></span></li>';

		}else 
			echo '<li><span class="error">'.$fname[$i].' -- is not an accepted file type<br></span></li>';
	}

	if (is_array($fname))
	$files = implode(', ',$fname);
	//else $files = $fname;

	$sqlInsert = mysql_query("INSERT INTO files (file_names) VALUES('$files')") or die (mysql_error());
}
}


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.