Jump to content

adding file uploads to already created webform


rlgriffo

Recommended Posts

Hi everyone,

 

I have a functioning web form which enters data into a mysql database when the submit button is pressed.  I added some image upload fields as an afterthought and I am having a lot of difficulty trying to find out how to make them "work"........

(most tutorials focus on image uploads without any other fields)

 

Below is the code which i have already compiled ( i am very new to mysql and php so it has taken me a lot of work to come this far). 

 

I have already added the html upload fields ready to go (these are at the very bottom of the form).

 

When I press the submit button I would like the images to be sent to a folder on the server and for the file's path to be put into the database  in its own column and maybe the file's size in another column.

 

I have tried posting this in several different forums and each time I have received absolutely no response at all...not sure why...

 

Thanks heaps in advance (i hope)

 

 

 

          <?php include 'header.php'; ?>

<div id="content">
      <div id="column2">
<?php 
#################################################################### 
################ DATABASE CONFIGURE ############################## 
#################################################################### 
$hostname = "localhost"; 
$db_user = "darwiner_rlgriff"; 
$db_password = "ellie1"; 
$db_table = "business"; 
$database = "darwiner_test"; 

# STOP HERE 
#################################################################### 
# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE 

$db = mysql_connect($hostname, $db_user, $db_password); 
mysql_select_db($database,$db); 
?> 


<?php 
if (isset($_REQUEST['Submit'])) { 
# THIS CODE TELL MYSQL TO INSERT THE DATA FROM THE FORM INTO YOUR MYSQL TABLE 
$sql = "INSERT INTO $db_table(username,business_name,industry,position,address1,address2,suburb,state,postcode,phonenumber,phonenumber2,email,website,keywords,business_catagory,info) values (
'".mysql_real_escape_string(stripslashes($_REQUEST['username']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['business_name']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['industry']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['position']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['address1']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['address2']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['suburb']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['state']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['postcode']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['phonenumber']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['phonenumber2']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['email']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['website']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['keywords']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['business_catagory']))."',
'".mysql_real_escape_string(stripslashes($_REQUEST['info']))."'
)";
if($result = mysql_query($sql ,$db)) { 
echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="http://www.webune.com/images/headers/default_logo.jpg"'; 
} else { 
echo "ERROR: ".mysql_error(); 
} 
} else { 
?> 
<h1>Enter New Business Details:</h1> 
<form method="post" enctype="multipart/form-data" action=""> 
Choose a Username:<br> 
<input type="text" name="username"> 
<br> <br>
Business Name: <br> 
<input type="text" name="business_name"> 
<br><br> 
Business Type / Industry: <br> 
<input type="text" name="industry"> 
<br><br> 
Your Position: <br> 
<input type="text" name="position"> 
<br><br> 
Address 1: <br> 
<input type="text" name="address1"> 
<br><br> 
Address 2: <br> 
<input type="text" name="address2"> 
<br><br> 
Suburb: <br> 
<input type="text" name="suburb"> 
<br><br> 
State: <br> 
  <select name="state">
    <option selected value="Northern Territory">NT - Northern Territory</option>
    <option value="Queensland">QLD - Queensland</option>
    <option value="New South Wales">NSW - New South Wales</option>
    <option value="Australian Capital Territory">ACT - Australian Capital Territory</option>
    <option value="Victoria">VIC - Victoria</option>
    <option value="South Australia">SA - South Australia</option>
    <option value="Western Australia">WA - Western Australia</option>
    <option value="Tasmania">TAS - Tasmania</option>
  </select><br><br> 
Postcode: <br> 
<input type="text" name="postcode"> 
<br><br> 
Phone Number 1: <br> 
<input type="text" name="phonenumber"> 
<br><br> 
Phone Number 2: <br> 
<input type="text" name="phonenumber2"> 
<br><br> 
Email Address: <br> 
<input type="text" name="email"> 
<br><br> 
Website Address: <br> 
<input type="text" name="website"> 
<br><br> 
Relevent Keywords: eg hairdresser, pub, cafe, stylest, designer, mechanic etc <br> 
<input type="text" name="keywords"> 
<br><br> 
Choose the most relevent catagory for your business to appear: <br> 
  <select name="business_catagory">
          <option value="accommodation">---  Accommodation:  ---</option>
        <option value="accommodation-bed_and_Breakfast">Accommodation -> Bed & Breakfast</option>
        <option value="accommodation-camping_and_caravaning">Accommodation -> Camping & Caravaning</option>
        <option value="accommodation-holiday_lettings" selected>Accommodation -> Holiday Lettings</option>
        <option value="accommodation-hotels_and-motels">Accommodation -> Hotels & Motels</option>
        <option value="accommodation-houseboats_and_cruisers">Accommodation -> Houseboats / Cruisers</option>
        <option value="accommodation-resorts">Accommodation -> Resorts</option>
        <option value="accommodation-self_catering">Accommodation -> Self Caterig</option>
  </select><br><br>
Information you would like displayed about your business: <br> 
<textarea name="info" cols=""></textarea>
<br><br>
Images you would like included in your listing:<br>
<input name="image1" type="file">
<br><br>
<input name="image2" type="file">
<br><br>
<input name="image3" type="file">
<br><br>
<input name="image4" type="file">
<br><br>
<input name="image5" type="file">
<br><br>
<input name="image6" type="file">
<br><br>
<input type="submit" name="Submit" value="Submit" id="submit"> 
</form> 

</div>
</div>

<?php include 'sidenav.php'; ?>

        <?php include 'footer.php'; ?>

here i adapted this code from http://www.reconn.us/content/view/30/51/

function upload($form_field)
{
$save_floder = "images/";
//reads the name of the file the user submitted for uploading
$image = $_FILES[$form_field]['name'];
//if it is not empty
if ($image) {
	//get the original name of the file from the clients machine
	$filename = stripslashes($_FILES[$form_field]['name']);
	//get the extension of the file in a lower case format
	$extension = getExtension($filename);
	$extension = strtolower($extension);
	//if it is not a known extension, we will suppose it is an error and will not  upload the file,
	//otherwise we will do more tests
	if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
		//print error message
		echo '<h1>Unknown extension!</h1>';
		$errors = 1;
	} else {
		//get the size of the image in bytes
		//$_FILES['image']['tmp_name'] is the temporary filename of the file
		//in which the uploaded file was stored on the server
		$size = filesize($_FILES[$form_field]['tmp_name']);

		//compare the size with the maxim size we defined and print error if bigger
		if ($size > MAX_SIZE * 1024) {
			echo '<h1>You have exceeded the size limit!</h1>';
			$errors = 1;
		}

		//we will give an unique name, for example the time in unix time format
		$image_name = time() . rand() . '.' . $extension;
		//the new name will be containing the full path where will be stored (images folder)
		$newname = $save_floder . $image_name;
		//we verify if the image has been uploaded, and print error instead
		$copied = copy($_FILES[$form_field]['tmp_name'], $newname);
		if (!$copied) {
			echo '<h1>Copy unsuccessfull!</h1>';
			$errors = 1;
		}
	}
}
}

add the function any where ni your code(top or bottom it doesn't matter it is created when the script starts) then write this after isset($_REQUEST['Submit']))

upload('image1');
upload('image2');
... to image 5

the images are saved as the current unix timestamp with a random number on the end and the correct file ext

 

Scott.

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.