Jump to content

Upload file using form and add path and other details to database.


fredted40x

Recommended Posts

Hi,

 

I have a form that allows a user to enter details and also to upload a image. The form works great and inserts into the database if i dont include the file input.

 

What i would like happen is:

 

The user enters details and selects a file

 

The file then gets uploaded to ../images and the path is then written to bedroom1 variable

 

The addresses and the bedroom1 variable that now holds the uploaded file path e.g. images/test.jpg is written to the database

 

Form

<form action="admin.php" method="POST">
	   <table>
	     <tr><td>
		     Address 1:
		 </td><td>
		     <input type="text" name="address1" id="address1" onfocus="selected(this)" onblur="notselected(this)">
		 </td><td>
		     <div id="dynamicText1">				 
             </div>
		 </td></tr>

		 <tr><td>
		     Address 2:
		 </td><td>
		     <input type="text" name="address2" id="address2" onfocus="selected(this)" onblur="notselected(this)">
		 </td><td>
		     <div id="dynamicText1">				 
             </div>
		 </td></tr>

		 <tr><td>
		     County:
		 </td><td>
		     <input type="text" name="county" id="county" onfocus="selected(this)" onblur="notselected(this)">
		 </td><td>
		     <div id="dynamicText1">				 
             </div>
		 </td></tr>

		 <tr><td>
		     Bedroom 1:
		 </td><td>
		     <input type="file" name="bedroom1" id="bedroom1" input name="uploadedfile">
		 </td><td>
		     <div id="dynamicText1">				 
             </div>
		 </td></tr>



		 <tr><td>
		     <input type="submit" name="submitaddapartment" value="Add" id="registerbutton">
		 </td></tr>

	   </table
           
        </form>

 

 

And the php code to add it to the database

 

 

<?php
  //get variables
  $submitaddapartment = $_POST['submitaddapartment'];
  $address1 = strip_tags($_POST['address1']);
  $address2 =  strip_tags($_POST['address2']);
  $county = strip_tags($_POST['county']);
  $bedroom1 = strip_tags($_POST['bedroom1']);
   
  
  

  if ($submitaddapartment) //if submit button was pressed
  {
    if ($address1&&$address2&&$county&&$bedroom1&&) //if fields arn't blank
    {
  include 'dbase.php'; //connect to database
  
  if ((($_FILES["bedroom1"]["type"] == "image/gif")
  || ($_FILES["bedroom1"]["type"] == "image/jpg")
  || ($_FILES["bedroom1"]["type"] == "image/pjpeg"))
  && ($_FILES["bedroom1"]["size"] < 20000000))
  {

  if ($_FILES["bedroom1"]["error"] > 0)
  {
  echo "Return Code: " . $_FILES["bedroom1"]["error"] . "<br />";
  }
  else
  {
  echo "Upload: " . $_FILES["bedroom1"]["name"] . "<br />";
  echo "Type: " . $_FILES["bedroom1"]["type"] . "<br />";
  echo "Size: " . ($_FILES["bedroom1"]["size"] / 1024) . " Kb<br />";
  echo "Temp file: " . $_FILES["bedroom1"]["tmp_name"] . "<br />";
  
  if (file_exists("../images/" . $_FILES["bedroom1"]["name"]))
      {
      echo $_FILES["bedroom1"]["name"] . " already exists. ";
      }
  else
      {
      move_uploaded_file($_FILES["bedroom1"]["tmp_name"],
      "/" . $_FILES["bedroom1"]["name"]);
      echo "Stored in: " . "../images/" . $_FILES["bedroom1"]["name"];
  $bedroom1=".'images/.' $_FILES["bedroom1"]["name"]";  
  }
  }
  }
  
  else
  {
  echo "Invalid file";
  }
  
  
  mysql_query ("INSERT INTO user VALUES(NULL,'$address1','$address2','$county','$bedroom1')");
          
    }
    else
    {
    }
  }    
  
?>

 

Any help would be greatly appreciated.

 

Thanks

 

Fred

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.