Jump to content

Problem with photo uploading form


MsKazza

Recommended Posts

Hi was hoping someone can tell me where i've gone wrong.  its a form for the user to upload a picture and then goes to the process.php page, however it doesn't seem to work, it calls the error and goes back to books.php (form page)

 

books.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Digital Scribe Books</title>
<link href="style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
</script>
<?php 
$directory_self = str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']); 

// make a note of the location of the upload handler script 
$uploadHandler = 'http://' . $_SERVER['HTTP_HOST'] . $directory_self . 'process.php'; 
?>


</head>

<body onload="MM_preloadImages('images/buttons/home_over.png','images/buttons/books_over.png','images/buttons/cards_over.png','images/buttons/letters_over.png')">


<div id="snow">
<div id="wrapper">


<div id="header">

<div id="logo"><img src="images/digital_scripe.png" width="218" height="91" /></div>


<div id="menu"><a href="index.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Home','','images/buttons/home_over.png',1)"><img src="images/buttons/home_act.png" name="Home" width="131" height="132" border="0" id="Home" /></a><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Books','','images/buttons/books_over.png',1)"><img src="images/buttons/books_act.png" name="Books" width="131" height="132" border="0" id="Books" /></a><a href="cards.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Cards','','images/buttons/cards_over.png',1)"><img src="images/buttons/cards_act.png" name="Cards" width="131" height="132" border="0" id="Cards" /></a><a href="letters.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Letters','','images/buttons/letters_over.png',1)"><img src="images/buttons/letters_act.png" name="Letters" width="131" height="132" border="0" id="Letters" /></a></div>
</div>




<div id="content">
  <div id="info_bar">
    <p>Send a personalised Christmas Card this year<br /> 
      <img src="images/cards/SampleCard.png" width="120" height="104" /><br />
      Available in packs of 10 or 20 from  € including envelopes, p&p.</p>
    <p>Simply choose the style you would like on the inside of the card.</p>
    <p>Choose which pack you would like.<br />
    </p>
    <p>Send us your drawing </p>
    <p>Your cards will be in the post with in 2-3 working days.</p>
  </div>

<form action="<?php echo $uploadHandler ?>" method="post" enctype="multipart/form-data" name="books" title="Santa_Book">
     <img src="images/book/Enter_your_details.png" width="400" height="60" /><br />
      
  
  
  
  <ul id="inline_list">
<input type="text" name="first_name" id="first_name" value="Text to be displayed here" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>


<label for="sur_name" class="inside">Name</label>
		  <input name="sur_name" type="text" id="sur_name" class="inside" />
          
	  <label for="sname">Surname</label>
	  <br />
	  Sex 
          <select name="sex" id="sex">
            <option selected="selected">Please Select....</option>
            <option value="girl">Girl</option>
            <option value="boy">Boy</option>
          </select> 
           <label for="age">Age</label>
          <input type="text" name="age" id="age" />
<br />
          

<label for="house_no">House No.</label>
<input type="text" name="house_no" id="house_no" />         
           <br />
                  			<label for="street">Street Name</label>

<input type="text" name="street" id="street" />
         <br />
                			<label for="town">Town</label>

<input type="text" name="town" id="town" />

        
      <br />
      <br />
       <select name="bro_sis_cous_friend1" id="bro_sis_cous_friend1">
         <option selected="selected">Please Select...</option>
         <option value="Brother1">Brother</option>
         <option value="Sister1">Sister</option>
         <option value="Cousin1">Cousin</option>
         <option value="Friend1">Friend</option>
       </select>
                             			<label for="bscf_name1">Friend / Sibling</label>

       <input type="text" name="bscf_name1" id="bscf_name1" />
        
      
     <br />
    <select name="Friend / Sibling">
      <option>Please Select...</option>
         <option value="Brother2">Brother</option>
         <option value="Sister2">Sister</option>
         <option value="Cousin2">Cousin</option>
         <option value="Friend2">Friend</option>
       </select>
       <label for="bscf_name2">Friend / Sibling</label>
       <input type="text" name="bscf_name2" id="bscf_name2" />
        
        
      <br />
                       			<label for="from_name">This book is from...</label>

          <input type="text" name="from_name" id="from_name" />
        
       <br />
          <input name="photo" type="file" id="photo" value="Upload Image" />
        
     <br />
   
       



  </ul>


  
<br />
  

<button type="submit" name="card_submit" id="submit_button">Continue</button>

</form>
  
  

  
  
  
  
  </div>


<div id="footer" class="clear"><div id="sign"><div id="sign_text">Personalised<br />
Books</div>
</div></div>

</div></div>






</body>
</html>

 

 

process.php page

 

<?php 
// Database connect
$con = mysql_connect("mysql1.myhost.ie","admin_book","root123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("book_test", $con);


//Parse Values from Coupon.php Form
$first_name  = mysql_real_escape_string(trim($_POST['first_name']));
$sur_name  = mysql_real_escape_string(trim($_POST['sur_name']));
$sex  = mysql_real_escape_string(trim($_POST['sex']));
$age  = mysql_real_escape_string(trim($_POST['age']));
$house_no = mysql_real_escape_string(trim($_POST['house_no']));
$street = mysql_real_escape_string(trim($_POST['street']));
$town = mysql_real_escape_string(trim($_POST['town']));
$bro_sis_cous_friend1 = mysql_real_escape_string(trim($_POST['bro_sis_cous_friend1']));
$bscf_name1 = mysql_real_escape_string(trim($_POST['bscf_name1']));
$bro_sis_cous_friend2 = mysql_real_escape_string(trim($_POST['bro_sis_cous_friend2']));
$bscf_name2 = mysql_real_escape_string(trim($_POST['bscf_name2']));
$from_name = mysql_real_escape_string(trim($_POST['from_name']));
$photo_link = mysql_real_escape_string(trim($_POST['photo_link']));

if ($sex == 'girl')
{
$his_her = 'her';
} 
else 
{
$his_her = 'his';
}

if ($sex == 'girl')
{
$him_her = 'her';
} 
else 
{
$him_her = 'his';
}

// make a note of the directory that will recieve the uploaded files
$uploadsDirectory = 'images/photos';

// make a note of the location of the upload form in case we need it
$uploadForm = 'books.php';

// make a note of the location of the success page
$uploadSuccess = 'thanks.php';




// Now let's deal with the upload

// possible PHP upload errors
$errors = array(1 => 'php.ini max file size exceeded', 
                2 => 'html form max file size exceeded', 
                3 => 'file upload was only partial', 
                4 => 'no file was attached');

// check the upload form was actually submitted else print form
isset($_POST['submit'])
or error('the upload form is neaded', $uploadForm);

// check for standard uploading errors
($_FILES[$fieldname]['error'] == 0)
or error($errors[$_FILES[$fieldname]['error']], $uploadForm);

// check that the file we are working on really was an HTTP upload
@is_uploaded_file($_FILES[$fieldname]['tmp_name'])
or error('not an HTTP upload', $uploadForm);

// validation... since this is an image upload script we 
// should run a check to make sure the upload is an image
@getimagesize($_FILES[$fieldname]['tmp_name'])
or error('only image uploads are allowed', $uploadForm);

// make a unique filename for the uploaded file and check it is 
// not taken... if it is keep trying until we find a vacant one
$now = time();
while(file_exists($uploadFilename = $uploadsDirectory.$now.'-'.$_FILES[$photo_link]['name']))
{
$now++;
}

// now let's move the file to its final and allocate it with the new filename
@move_uploaded_file($_FILES[$photo_link]['tmp_name'], $uploadFilename)
or error('receiving directory insuffiecient permission', $uploadForm);

// make an error handler which will be used if the upload fails
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL=\"$location\"");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
'<html lang="en">'."\n".
'	<head>'."\n".
'		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
'		<link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
'	<title>Upload error</title>'."\n\n".
'	</head>'."\n\n".
'	<body>'."\n\n".
'	<div id="Upload">'."\n\n".
'		<h1>Upload failure</h1>'."\n\n".
'		<p>An error has occured: '."\n\n".
'		<span class="red">' . $error . '...</span>'."\n\n".
'	 	The upload form is reloading</p>'."\n\n".
'	 </div>'."\n\n".
'</html>';
exit;
} // end error handler


$sql="INSERT INTO details (first_name, sur_name, sex, age, house_no, street, town, bro_sis_cous_friend1, bscf_name1, andy, bro_sis_cous_friend2, bscf_name2, his_her, him_her, from_name, photo_link)
VALUES
('$first_name','$sur_name','$sex','$age','$house_no','$street','$town','$bro_sis_cous_friend1','$bscf_name1','and','$bro_sis_cous_friend2','$bscf_name2','$his_her','$him_her','$from_name','$photo_link')";

echo 'Thank you '. $first_name . ' for entering your details bro.<br />';
echo 'surname is : '. $sur_name . '.<br />';
echo 'sex is :  '. $sex . '.<br />';
echo 'age is: '. $age . ' .<br />';
echo 'bscf friend 1 is :  '. $bro_sis_cous_friend1 . '.<br />';
echo 'his_her is : '. $his_her . ' .<br />';
echo 'him_her is :  '. $him_her . '.<br />';

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }

header("Location: thanks.php");





?>

 

pls pls help i'm soooo stuck lol

 

MsKazza

Link to comment
Share on other sites

id doesn't actually give any specific error message just the error function that is included in the code :

 

// make an error handler which will be used if the upload fails
function error($error, $location, $seconds = 5)
{
header("Refresh: $seconds; URL=\"$location\"");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n".
'"http://www.w3.org/TR/html4/strict.dtd">'."\n\n".
'<html lang="en">'."\n".
'	<head>'."\n".
'		<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n".
'		<link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n".
'	<title>Upload error</title>'."\n\n".
'	</head>'."\n\n".
'	<body>'."\n\n".
'	<div id="Upload">'."\n\n".
'		<h1>Upload failure</h1>'."\n\n".
'		<p>An error has occured: '."\n\n".
'		<span class="red">' . $error . '...</span>'."\n\n".
'	 	The upload form is reloading</p>'."\n\n".
'	 </div>'."\n\n".
'</html>';
exit;
} // end error handler

Link to comment
Share on other sites

ok someone said that this is far too complicated so i've tried it this way :

 

form page


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Digital Scribe Books</title>
<link href="style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
</script>


</head>

<body onload="MM_preloadImages('images/buttons/home_over.png','images/buttons/books_over.png','images/buttons/cards_over.png','images/buttons/letters_over.png')">


<div id="snow">
<div id="wrapper">


<div id="header">

<div id="logo"><img src="images/digital_scripe.png" width="218" height="91" /></div>


<div id="menu"><a href="index.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Home','','images/buttons/home_over.png',1)"><img src="images/buttons/home_act.png" name="Home" width="131" height="132" border="0" id="Home" /></a><a href="#" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Books','','images/buttons/books_over.png',1)"><img src="images/buttons/books_act.png" name="Books" width="131" height="132" border="0" id="Books" /></a><a href="cards.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Cards','','images/buttons/cards_over.png',1)"><img src="images/buttons/cards_act.png" name="Cards" width="131" height="132" border="0" id="Cards" /></a><a href="letters.html" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('Letters','','images/buttons/letters_over.png',1)"><img src="images/buttons/letters_act.png" name="Letters" width="131" height="132" border="0" id="Letters" /></a></div>
</div>




<div id="content">
  <div id="info_bar">
    <p>Send a personalised Christmas Card this year<br /> 
      <img src="images/cards/SampleCard.png" width="120" height="104" /><br />
      Available in packs of 10 or 20 from  € including envelopes, p&p.</p>
    <p>Simply choose the style you would like on the inside of the card.</p>
    <p>Choose which pack you would like.<br />
    </p>
    <p>Send us your drawing </p>
    <p>Your cards will be in the post with in 2-3 working days.</p>
  </div>

<form enctype="multipart/form-data" action="process.php" method="POST" name="books" title="Santa_Book">
     <img src="images/book/Enter_your_details.png" width="400" height="60" /><br />
      
  
  
  
  <ul id="inline_list">
<input type="text" name="first_name" id="first_name" value="Text to be displayed here" onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>


<label for="sur_name" class="inside">Name</label>
		  <input name="sur_name" type="text" id="sur_name" class="inside" />
          
	  <label for="sname">Surname</label>
	  <br />
	  Sex 
          <select name="sex" id="sex">
            <option selected="selected">Please Select....</option>
            <option value="girl">Girl</option>
            <option value="boy">Boy</option>
          </select> 
           <label for="age">Age</label>
          <input type="text" name="age" id="age" />
<br />
          

<label for="house_no">House No.</label>
<input type="text" name="house_no" id="house_no" />         
           <br />
                  			<label for="street">Street Name</label>

<input type="text" name="street" id="street" />
         <br />
                			<label for="town">Town</label>

<input type="text" name="town" id="town" />

        
      <br />
      <br />
       <select name="bro_sis_cous_friend1" id="bro_sis_cous_friend1">
         <option selected="selected">Please Select...</option>
         <option value="Brother1">Brother</option>
         <option value="Sister1">Sister</option>
         <option value="Cousin1">Cousin</option>
         <option value="Friend1">Friend</option>
       </select>
                             			<label for="bscf_name1">Friend / Sibling</label>

       <input type="text" name="bscf_name1" id="bscf_name1" />
        
      
     <br />
    <select name="Friend / Sibling">
      <option>Please Select...</option>
         <option value="Brother2">Brother</option>
         <option value="Sister2">Sister</option>
         <option value="Cousin2">Cousin</option>
         <option value="Friend2">Friend</option>
       </select>
       <label for="bscf_name2">Friend / Sibling</label>
       <input type="text" name="bscf_name2" id="bscf_name2" />
        
        
      <br />
                       			<label for="from_name">This book is from...</label>

          <input type="text" name="from_name" id="from_name" />
        
       <br />
          <input name="uploadedfile" type="file" id="uploadedfile" value="Upload Image" />
        
     <br />
   
       



  </ul>


  
<br />
  

<input type="submit" value="Continue" />

</form>
  
  

  
  
  
  
  </div>


<div id="footer" class="clear"><div id="sign"><div id="sign_text">Personalised<br />
Books</div>
</div></div>

</div></div>






</body>
</html>

 

 

process.php page

 

<?php 
// Database connect
$con = mysql_connect("mysql1.myhost.ie","admin_book","root123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("book_test", $con);




//Parse Values from Coupon.php Form
$first_name  = mysql_real_escape_string(trim($_POST['first_name']));
$sur_name  = mysql_real_escape_string(trim($_POST['sur_name']));
$sex  = mysql_real_escape_string(trim($_POST['sex']));
$age  = mysql_real_escape_string(trim($_POST['age']));
$house_no = mysql_real_escape_string(trim($_POST['house_no']));
$street = mysql_real_escape_string(trim($_POST['street']));
$town = mysql_real_escape_string(trim($_POST['town']));
$bro_sis_cous_friend1 = mysql_real_escape_string(trim($_POST['bro_sis_cous_friend1']));
$bscf_name1 = mysql_real_escape_string(trim($_POST['bscf_name1']));
$bro_sis_cous_friend2 = mysql_real_escape_string(trim($_POST['bro_sis_cous_friend2']));
$bscf_name2 = mysql_real_escape_string(trim($_POST['bscf_name2']));
$from_name = mysql_real_escape_string(trim($_POST['from_name']));
$photo = mysql_real_escape_string(trim($_POST['uploadedfile']));

if ($sex == 'girl')
{
$his_her = 'her';
} 
else 
{
$his_her = 'his';
}

if ($sex == 'girl')
{
$him_her = 'her';
} 
else 
{
$him_her = 'his';
}


$sql="INSERT INTO details (first_name, sur_name, sex, age, house_no, street, town, bro_sis_cous_friend1, bscf_name1, andy, bro_sis_cous_friend2, bscf_name2, his_her, him_her, from_name, photo)
VALUES
('$first_name','$sur_name','$sex','$age','$house_no','$street','$town','$bro_sis_cous_friend1','$bscf_name1','and','$bro_sis_cous_friend2','$bscf_name2','$his_her','$him_her','$from_name','$photo')";


// Where the file is going to be placed 
$target_path = "uploads/";

/* Add the original filename to our target path.  
Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}



echo 'Thank you '. $first_name . ' for entering your details bro.<br />';
echo 'surname is : '. $photo . '.<br />';
echo 'sex is :  '. $sex . '.<br />';
echo 'age is: '. $age . ' .<br />';
echo 'bscf friend 1 is :  '. $bro_sis_cous_friend1 . '.<br />';
echo 'his_her is : '. $his_her . ' .<br />';
echo 'him_her is :  '. $him_her . '.<br />';

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }







?>

 

when i run just the upload code it works fine, but when i add it to the rest of the process.php page it stops working.  I output i get it:

 

There was an error uploading the file, please try again!Thank you witch for entering your details bro.

surname is : .

sex is : girl.

age is: 55 .

bscf friend 1 is : Sister1.

his_her is : her .

him_her is : her.

 

I think i just have the code in the wrong order but well......

 

thanks

MsKazza

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.