Jump to content

Form - I need to add upload photo capability


tqla

Recommended Posts

Hello. Can some one point me in the right direction?

 

I made a form that collects general information, validates it and sends it to a database. Simple.

 

I want to add an upload field to it that will allow the user to attach a photo.

 

I'm stuck. I know how to upload a file by itself, but how do I integrate it into an existing form?

 

If the photo gets uploaded to an "uploads" folder and the form information to the database. How do I know which database entry each photo belongs too? Should I upload directly to the Database itself instead?

 

Is there a premade solution to do this so I can save time?

 

Thanks!

There should not be any problems with the file upload being in a from with other input fields.  Use the same code you normally would.  Here is an example from a website I am working on:

 

<?php
// In this, there are $number fields.  I work my way up from 1, looping until all the files are uploaded.
for ($n = 1;$n <= $number;$n++) {


// Give variables a name to be used in gathering data from $_POST

$filvar = "file$n";
$namvar = "name$n";
$desvar = "description$n";
$yeavar = "year$n";
$monvar = "month$n";
$picvar = "picname$n";
$typvar = "flo/ani$n";

// Upload the file
$destination = '..\images'."\\".$_FILES[$filvar]['name'];
move_uploaded_file($_FILES[$filvar]['tmp_name'],$destination);

// I process all of the other form fields later.
?>

 

I am unsure about your question:

If the photo gets uploaded to an "uploads" folder and the form information to the database. How do I know which database entry each photo belongs too? Should I upload directly to the Database itself instead?

If you want the filename added to the database, just add a column for filenames, and input the filename.  If the location is the same, just automatically include the location in your code.  If it changes, you will have to include the location in the database.

Thanks 947740.

 

Let me explain with my code.

 

Here is my form:

<link href="style.css" rel="stylesheet" type="text/css">
<div id="containerDiv">
<form name="form1" method="post" action="checkAll.php" enctype="multipart/form-data">
  <table  border="0" align="center" cellpadding="0" cellspacing="0">
      <tr align="left" valign="top">
      <td colspan="2"><h1 class="style1"><br>
Online Registration Application </h1>        </td>
    </tr>
      <tr align="left" valign="top">
      <td colspan="2"><p> </p>        </td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>YOU MUST BE AT LEAST 18 YEARS OLD TO REGISTER. PLEASE TELL US YOUR AGE AND DATE OF BIRTH:</p>        </td>
    </tr>
    <tr align="left" valign="top">
      <td><p>AGE:</p></td>
      <td><p>
        <input name="age" type="text" id="age" value="<?php echo ($_POST['age']) ?>" size="3" maxlength="3">
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>D.O.B.:</p></td>
      <td><p>
        <input name="dob" type="text" id="dob" value="<?php echo ($_POST['dob']) ?>" size="10" maxlength="10">
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>NAME:</p></td>
      <td><p>
        <input name="name" type="text" id="name" value="<?php echo ($_POST['name']) ?>" size="50" maxlength="50">
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>ADDRESS:</p></td>
      <td><p>
        <input name="address" type="text" id="address" value="<?php echo ($_POST['address']) ?>" size="50" maxlength="100">
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>CITY/STATE/ZIP:</p></td>
      <td><p>
        <input name="citystatezip" type="text" id="citystatezip" value="<?php echo ($_POST['citystatezip']) ?>" size="50" maxlength="100">
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>EMAIL:</p></td>
      <td><p>
        <input name="email" type="text" id="email" value="<?php echo ($_POST['email']) ?>" size="50" maxlength="50">
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>CELL#:</p></td>
      <td><p>
        <input name="cell" type="text" id="cell" value="<?php echo ($_POST['cell']) ?>" size="14" maxlength="14">
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>EMERGENCY CONTACT:</p></td>
      <td><p>
        <input name="emergencycontact" type="text" id="emergencycontact" value="<?php echo ($_POST['emergencycontact']) ?>" size="50" maxlength="50">
      </p></td>
    </tr>
	    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>EACH INDIVIDUAL MUST COMPLETE A REGISTRATION FORM, IF YOU ARE PLANNING TO JOIN THE CARAVAN WITH OTHERS, PLEASE LIST THEIR NAMES HERE:</p></td>
    </tr>
	    <tr align="left" valign="top">
      <td colspan="2"><p>
        <textarea name="caravan" cols="50" rows="5" id="caravan"><?php echo ($_POST['caravan']) ?></textarea>
      </p></td>
    </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
	    <tr align="left" valign="top">
      <td colspan="2"><p>.

  </p></td>
    </tr>
	    <tr align="left" valign="top">
      <td><p>I do not require a return seat </p></td>
      <td><input name="noseat" type="checkbox" id="noseat" value="noseat"
			<?php
			$OK = isset($_POST['noseat']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>			  
	  /></td>
      </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
	    <tr align="left" valign="top">
      <td colspan="2"><p>PLEASE WRITE A BRIEF STATEMENT ABOUT WHY YOU WANT TO PARTICIPATE</p></td>
    </tr>
	    <tr align="left" valign="top">
      <td colspan="2"><p>
        <textarea name="statement" cols="50" rows="5" id="statement"><?php echo ($_POST['statement']) ?></textarea>
      </p></td>
    </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>PLEASE ATTACH A CLEAR AND RECENT PHOTOGRAPH OF YOURSELF</p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><input type="file" name="photo"></td>
    </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>DO YOU HAVE ANY SPECIAL DIETARY NEEDS WE SHOULD KNOW OF?</p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>I’m omnivorous, just feed me.</p></td>
      <td><p>
        <input name="omnivorous" type="checkbox" id="omnivorous" value="omnivorous"
			<?php
			$OK = isset($_POST['omnivorous']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>	/>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>I’m vegetarian but eat dairy.</p></td>
      <td><p>
        <input name="vegetarian" type="checkbox" id="vegetarian" value="vegetarian"
			<?php
			$OK = isset($_POST['vegetarian']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>			
	/>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>I’m strictly vegan.</p></td>
      <td><p>
        <input name="vegan" type="checkbox" id="vegan" value="vegan"
			<?php
			$OK = isset($_POST['vegan']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>			
	/>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>Kosher</p></td>
      <td><p>
        <input name="kosher" type="checkbox" id="kosher" value="kosher"
			<?php
			$OK = isset($_POST['kosher']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>	
	/>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>Other, please explain:</p></td>
      <td><p>
        <textarea name="mealother" cols="50" rows="5" id="mealother"><?php echo ($_POST['mealother']) ?></textarea>
      </p></td>
    </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>DO YOU HAVE ANY MEDICAL OR PHYSICAL CONDITIONS THAT WE SHOULD KNOW OF TO ENSURE YOUR WELLBEING AND SAFETY DURING THE CARAVAN?</p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>No</p></td>
      <td><p>
        <input name="caravancondition" type="checkbox" id="caravancondition" value="caravancondition"
			<?php
			$OK = isset($_POST['caravancondition']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>			
	/>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>Yes, please explain:</p></td>
      <td><p>
        <textarea name="caravanconditionexplain" cols="50" rows="5" id="caravanconditionexplain"><?php echo ($_POST['caravanconditionexplain']) ?></textarea>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>IF YOU KNOW YOUR BLOOD TYPE, LET US KNOW:</p></td>
      <td><p>
        <input name="bloodtype" type="text" id="bloodtype" value="<?php echo ($_POST['bloodtype']) ?>" size="20" maxlength="20">
      </p></td>
    </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>SORRY, BUT WE CANNOT ACCOMMODATE PETS ON THE CARAVAN, WE WILL MAKE OUR BEST EFFORT TO ACCOMMODATE INDIVIDUALS WITH SERVICE ANIMALS. CHECK HERE IF YOU WISH TO BRING A SERVICE ANIMAL:</p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>Yes, I have a service animal:</p></td>
      <td><p>
        <input name="animal" type="text" id="animal" value="<?php echo ($_POST['animal']) ?>" size="25" maxlength="25">
      </p></td>
    </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>WE WILL BE CAMPING DURING THE CARAVAN, PLEASE BRING YOUR OWN TENT. IF YOU DON’T HAVE A TENT, LET US KNOW NOW.</p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>I will bring my own tent.</p></td>
      <td><p>
        <input name="yestent" type="checkbox" id="yestent" value="yestent"
			<?php
			$OK = isset($_POST['yestent']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>			
	/>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td><p>I will not be bringing my own tent.</p></td>
      <td><p>
        <input name="notent" type="checkbox" id="notent" value="notent"
			<?php
			$OK = isset($_POST['notent']) ? true : false;
			if ($OK && (isset($blank_array) || isset($bad_format))) { ?>
			checked="checked"
			<?php } ?>			
	/>
      </p></td>
    </tr>
		    <tr align="left" valign="top">
      <td colspan="2"><p> </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p> </p>
      <p><br>
      </p></td>
    </tr>
    <tr align="left" valign="top">
      <td colspan="2"><p>
        <input type="submit" name="Submit" value="Submit">
      </p>
        <p>         </p></td>
    </tr>
  </table>
</form>
</div>

 

 

Here is my script:

<?php require_once('Connections/Auth.php');  
session_start();  
echo "<div id=\"containerDiv\">";
echo "<div id=\"box\">";
echo "<div align=\"center\"><br />";

  /* set up array containing all the fields */
  $labels = array ( "age" => "Age",
                    "dob" => "Date of Birth",
                    "name" => "Name",
                    "address" => "Address",
                    "citystatezip" => "City, State, Zip",
                    "email" => "Email",
                    "cell" => "Cell",					
                    "emergencycontact" => "Emergency Contact"
				);
  foreach ($_POST as $field => $value)
  {
    /* check each field except these for blank fields */
    if ( $value == "" )
    {
       if ($field != "caravan" and 
   		$field != "noseat" and 
   		$field != "statement" and 
   		$field != "omnivorous" and 
		$field != "vegetarian" and 
		$field != "vegan" and 
		$field != "kosher" and
   		$field != "mealother" and 
   		$field != "caravancondition" and 
   		$field != "caravanconditionexplain" and 
		$field != "bloodtype" and 
		$field != "animal" and 
		$field != "yestent" and 			 
		$field != "notent")
       {
          $blank_array[] = $field;
       }
    }
    /* check names for invalid formats. */
    elseif ($field == "" or 
		$field == "" or 
		$field == "" or 
		$field == "" or 
		$field == "")
    {
        if (!ereg("^[A-Za-z'& -\.,]{1,50}$",$_POST[$field]) )
        {
             $bad_format[] = $field;
        }
    }
    /* check phone for invalid format. */
    elseif ($field == "cell" or
		$field == "")
    {
      if(!ereg("^[0-9)( (-|\.)]{7,20}(([xX]|(ext)|(ex))?[ (-|\.)]?[0-9]{1,7})?$",$value))
      {
           $bad_format[] = $field;
      }
    }
/* check email for invalid format. */
    elseif ($field == "email")
    {
      if(!ereg("^.+@.+\\..+$",$value))
      {
           $bad_format[] = $field;
      }
    }
  }
  /* if any fields are not okay, display error message and form */
  if(@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
  {
    if(@sizeof($blank_array) > 0)
    {
        /* display message for missing information */
        echo "<table width=\"700\" border=\"0\" cellspacing=\"5\" bgcolor=\"#CCCCCC\"><tr><td style='text-align: left;'><alert><b>You didn't fill in one or more required 
	         fields. <br>You must enter:</b></alert><br></td></tr></table>";
        /* display list of missing information */
        foreach($blank_array as $value)
        {
           echo "<table width=\"700\" border=\"0\" cellspacing=\"5\" bgcolor=\"#CCCCCC\"><tr><td style='text-align: left;'><alert>   {$labels[$value]}</alert><br></td></tr></table>";
        }
    }
    if(@sizeof($bad_format) > 0)
    {
        /* display message for bad information */
        echo "<table width=\"700\" border=\"0\" cellspacing=\"5\" bgcolor=\"#CCCCCC\"><tr><td style='text-align: left;'><br><alert><b>One or more fields have information that appears to be 
                 incorrect. <br>Correct the format for:</b></alert><br></td></tr></table>";
        /* display list of bad information */
        foreach($bad_format as $value)
        {
           echo "<table width=\"700\" border=\"0\" cellspacing=\"5\" bgcolor=\"#CCCCCC\"><tr><td style='text-align: left;'><alert>   {$labels[$value]}</alert><br></td></tr></table>";
        }
    }
          include("index.php");

echo "</div>";
echo "</div>";

  exit(); 
}

  		/* if data is good */


	 $age = $_POST['age'];
	 $dob = $_POST['dob'];
	 $name = $_POST['name'];
	 $address = $_POST['address'];
	 $citystatezip = $_POST['citystatezip'];
	 $email = $_POST['email'];
	 $cell = $_POST['cell'];
	 $emergencycontact = $_POST['emergencycontact'];
	 $caravan = $_POST['caravan'];
	 $noseat = $_POST['noseat'];
	 $statement = $_POST['statement'];
	 $omnivorous = $_POST['omnivorous']; 
	 $vegetarian = $_POST['vegetarian']; 
	 $vegan = $_POST['vegan'];
	 $kosher = $_POST['kosher'];
	 $mealother = $_POST['mealother'];
	 $caravancondition = $_POST['caravancondition'];
	 $caravanconditionexplain = $_POST['caravanconditionexplain'];
	 $bloodtype = $_POST['bloodtype'];
	 $animal = $_POST['animal']; 
	 $yestent = $_POST['yestent']; 
	 $notent = $_POST['notent'];
	 $photo = $_POST['photo'];

/* Test data 

echo "$age, 
$dob, 
$name, 
$address,
$citystatezip,
$email,
$cell,
$emergencycontact,
$caravan,
$noseat,
$statement,
$omnivorous,
$vegetarian, 
$vegan,
$kosher,
$mealother,
$caravancondition,
$caravanconditionexplain,
$bloodtype,
$animal, 
$yestent, 
$notent
";
*/



$connection = mysql_connect($hostname_Auth, $username_Auth, $password_Auth) 
or die ("Couldn't connect to server.");
$db = mysql_select_db($database_Auth, $connection)
or die ("Couldn't select database."); 



/* Insert Data in DB */


      	$today = date("Y-m-d");
  	$date=time();   
	$sql = "INSERT INTO dhc 

	(createDate,
	 age,
	 dob,
	 name,
	 address,
	 citystatezip,
	 email,
	 cell,
	 emergencycontact,
	 caravan,
	 noseat,
	 statement,
	 omnivorous, 
	 vegetarian,
	 vegan,
	 kosher,
	 mealother,
	 caravancondition,
	 caravanconditionexplain,
	 bloodtype,
	 animal, 
	 yestent, 
	 notent,
	 photo)

	VALUES

	('$today',
	'$age', 
	'$dob', 
	'$name', 
	'$address',
	'$citystatezip',
	'$email',
	'$cell',
	'$emergencycontact',
	'$caravan',
	'$noseat',
	'$statement',
	'$omnivorous',
	'$vegetarian', 
	'$vegan',
	'$kosher',
	'$mealother',
	'$caravancondition',
	'$caravanconditionexplain',
	'$bloodtype',
	'$animal', 
	'$yestent', 
	'$notent',
	'$photo')"; 

mysql_query($sql) or die(mysql_error());



?>

 

 

You'll notice that in the script there is a "FILE UPLOAD" part.

 

What is the code to upload the photo? And how do I get an entry in the Photo field so that I can know what photo goes with what database entry? How do I prevent pictures with the same name from overwriting another.

 

Yikes! Am I asking too much?  :-[

As far as the database goes, I would just add another column with the name "photoname".  In the script, insert the photo name into the "photoname" column.  This way, the photo will be with the rest of the information, such as name, dietary needs, etc.

 

The code for uploading a file:

 

$destination = 'folder location relative to your current directory'."\\".$_FILES['photo']['name'];
move_uploaded_file($_FILES['photo']['tmp_name'],$destination);

 

The last question involving conflicting filenames.  A solution I have seen elsewhere would be to append a random number to the end of the filename.  You could even use a session id if you wanted too...

Archived

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

×
×
  • 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.