p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   PHP How-To (http://p2p.wrox.com/forumdisplay.php?f=98)
-   -   How to: Upload image to server and save image name in db with other form info (http://p2p.wrox.com/showthread.php?t=76702)

Clint October 22nd, 2009 06:00 AM

How to: Upload image to server and save image name in db with other form info
 
Hello all,

Could you please advise me on the following:
I have created a from to insert data into mysql database, including an uploadimage on the form. The image gets uploaded to the server and the name of image to the db, but I keep getting "Sorry, there was a problem uploading your file" message.

Here is the form and the code to process the data I have:

Code:


<html>
<head>
<title>Insert business for sale</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="../js/country.js"></script>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
  <tr>
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr>
    <td width="160" valign="top"><p>&nbsp;</p>
      <p>&nbsp; </p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    <td width="732" valign="top"><p>
        </p>
      <h3 class="titlehdr">Add details for business for sale here:</h3>
   
          Or go back to Admin Panel <a href="admin_main.php">here</a>.<br>
      <form method="post" action="addcourse.php" enctype="multipart/form-data">
        <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">
          <tr>
            <td colspan="2" height="49"><font color="#FF0000">Course Name</font><font color="#CC0000">*</font><br>
              <input name="course" type="text" id="course" size="40">
            </td>
            <td width="4"></td>
          </tr>
          <tr>
            <td colspan="2" height="21">&nbsp;</td>
            <td></td>
          </tr>
          <tr>
            <td colspan="2" height="114"><font color="#FF0000">Contact Address
              (with ZIP)</font><font color="#CC0000">*</font><br>
              <textarea name="address" cols="40" rows="4" id="address"></textarea>
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="21" valign="top" colspan="2"><font color="#FF0000"><b>Company
              details:</b></font></td>
            <td></td>
          </tr>
          <tr>
            <td height="59" valign="top" colspan="2">
              <textarea name="profile" cols="80" rows="8" id="profile" onkeyup="if (this.value.length > 300) { alert('Character limit has been reached!'); this.value = this.value.substr(0,5); }"></textarea>
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="21" valign="top" colspan="2">&nbsp;</td>
            <td></td>
          </tr>
          <tr>
            <td height="32" width="125" valign="top"><font color="#FF0000">Contact
              Person</font></td>
            <td width="482" valign="top">
              <input type="text" name="contact" id="contact">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="21" colspan="2" valign="top"><font color="#FF0000" size="1">Please
              select your country/state</font> <font color="#FF0000">*</font></td>
            <td></td>
          </tr>
          <tr>
            <td height="27" valign="top">
              <select id='countrySelect' name='country' onchange='populateState()'>
              </select>
            </td>
            <td valign="top">
              <select id='stateSelect' name='state'>
              </select>
              <script type="text/javascript">initCountry('PLS'); </script>
            <td></td>
          </tr>
          <tr>
            <td height="40" valign="top"><font color="#FF0000">City</font></td>
            <td valign="top">
              <input type="text" name="city" id="city">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Phone</font><font color="#CC0000">*</font>
            </td>
            <td valign="top">
              <input name="tel" type="text" id="tel">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Holes</font> </td>
            <td valign="top">
              <input name="holes" type="text" id="holes">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Par</font> </td>
            <td valign="top">
              <input name="par" type="text" id="par">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Designer</font>
            </td>
            <td valign="top">
              <input name="designer" type="text" id="designer">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Course Distance</font>
            </td>
            <td valign="top">
              <input name="distance" type="text" id="distance">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Fairways Grass</font>
            </td>
            <td valign="top">
              <input name="fairways" type="text" id="fairways">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Greens Grass</font>
            </td>
            <td valign="top">
              <input name="greens" type="text" id="greens">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Website </font></td>
            <td valign="top">
              <input name="web" type="text" id="web" class="optional defaultInvalid url">
              <span class="example">http://www.example.com</span></td>
            <td></td>
          </tr>
          <tr>
            <td height="25" colspan="2" valign="top">&nbsp;</td>
            <td></td>
          </tr>
          <tr>
            <td height="34" valign="top"><font color="#FF0000">Email Address:</font><span class="required"><font color="#CC0000">*</font></span>
            </td>
            <td valign="top">
              <input name="usr_email" type="text" id="usr_email">
            </td>
            <td></td>
          </tr>
          <tr>
            <td valign="top" height="41"><font color="#FF0000">Upload Avatar</font></td>
            <td valign="top">
              <input type="hidden" name="size" value="350000">
              <input type="file" name="photo">
            </td>
            <td></td>
          </tr>
          <tr>
            <td height="34"></td>
            <td></td>
            <td></td>
          </tr>
          <tr>
            <td height="72" valign="top">
              <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Course"/>
            </td>
            <td></td>
            <td></td>
          </tr>
        </table>
        <p align="center">&nbsp; </p>
      </form>
   
         
      </td>
    <td width="196" valign="top">&nbsp;</td>
  </tr>
  <tr>
    <td colspan="3">&nbsp;</td>
  </tr>
</table>

</body>
</html>

and the second part:

Code:

<?php
include "../dbc.php";

//This is the directory where images will be saved
$target = "http://www.supremeicon.co.za/market/Thumbs/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$course=$_POST['course'];
$address=$_POST['address'];
$profile=$_POST['profile'];
$contact=$_POST['contact'];
$country=$_POST['country'];
$state=$_POST['state'];
$city=$_POST['city'];
$tel=$_POST['tel'];
$holes=$_POST['holes'];
$par=$_POST['par'];
$designer=$_POST['designer'];
$distance=$_POST['distance'];
$fairways=$_POST['fairways'];
$greens=$_POST['greens'];
$web=$_POST['web'];
$user_email=$_POST['user_email'];
$pic=($_FILES['photo']['name']);

//Writes the information to the database
mysql_query("INSERT INTO golflist (course,address,profile,contact,country,state,city,tel,holes,par,designer,distance,fairways,greens,web,user_email,photo,date)
VALUES ('$course', '$address', '$profile','$contact','$country','$state','$city','$tel','$holes','$par','$designer','$distance','$fairways','$greens','$web','$user_email','$pic',now())") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the

directory";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file.";
}
?>

Can anybody see where I am going wrong here? If not everywhere [:I]

Thanks for your time. I do appreciate it!

surya October 26th, 2009 05:50 AM

Check your destination folder permission!
It should be as 705(in Linux sys), writable


All times are GMT -4. The time now is 04:40 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.