views:

8620

answers:

10

Hi I have read many forums and websites that tell you how to upload an image to a server and I have managed to get this working, I can upload a file to a my server but storing the file name does work on the following example I found and I also need to create a form that allows more data to be entered to the database. I am stuck with this as a have done much php before. I have come to the end of trying different websites tutorals with no much success could anyone please help me! I need it done for a project im doing.

I am basicly trying to make a CMS that allows users to upload a photo of a band member and have information stored about them so that it can be displayed on a webpage for the public to view.

Hope that all makes sence.

Cheers

Jimmy


My Table looks like this:

Field Type Null Default id int(10) No nameMember varchar(25) No bandMember text No photo varchar(30) No aboutMember text No otherBands text No

The form I want will look like this:

   <h1>Adding a new Band Member or Affiliate</h1>
      <form method="post" action="addMember.php" enctype="multipart/form-data">
       <p>
              Please Enter the Band Members Name.
            </p>
            <p>
              Band Member or Affiliates Name:
            </p>
            <input type="text" name="nameMember"/>
            <p>
              Please Enter the Band Members Position. Example:Drums.
            </p>
            <p>
              Member's Position:
            </p>
            <input type="text" name="bandMember"/>
            <p>
              Please Upload a Photo in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten!
            </p>
            <p>
              Photo:
            </p>
            <input type="file" name="filep" size=35 />
            <p>
              Please Enter any other information about the band member here.
            </p>
            <p>
              Other Member Information:
            </p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
            <p>
              Please Enter any other Bands the Member has been in.
            </p>
            <p>
              Other Bands:
            </p>
            <input type="text" name="otherBands" size=30 />
            <br/>
            <br/>
            <input TYPE="submit" title="Add data to the Database" value="Add Member"/>
          </form>

The Example that uploads an Image to the server and only, that is this:

<?

if ($_POST["action"] == "Load")
{
$folder = "images/";

move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".$_FILES["filep"]["name"]);

echo "
<p align=center>File ".$_FILES["filep"]["name"]."loaded...";

$result = mysql_connect("localhost", "******", "*****") or die ("Could not save image name

Error: " . mysql_error());

mysql_select_db("project") or die("Could not select database");
mysql_query("INSERT into dbProfiles (photo) VALUES('".$_FILES['filep']['name']."')");
if($result) { echo "Image name saved into database

"; }

}

?>

And the Examples form I have to use is this:

<form action=addMember.php method=post enctype="multipart/form-data">
<table border="0" cellspacing="0" align=center cellpadding="3" bordercolor="#cccccc">
<tr>
<td>File:</td>
<td><input type="file" name="filep" size=45></td>
</tr>
<tr>
<td colspan=2><p align=center>
<input type=submit name=action value="Load">
</td>
</tr>
</table>
</form>

Thanks again for any help.

Ps. Images file is open for writing to.

A: 

Can I suggest being more specific in the information you're looking for. You have the image uploading working, correct? So, it's the form that you're questioning? What exactly is not working?

As a side note I would also recommend you take a look at your db structure. You could normalize the data a bit and make it easier to modify in the future.

A: 

If you want to input more data into the form, you simply access the submitted data through $_POST.

If you have

<input type="text" name="firstname" />

you access it with

$firstname = $_POST["firstname"];

You could then update your query line to read

mysql_query("INSERT INTO dbProfiles (photo,firstname)
             VALUES('{$filename}','{$firstname}')");

Note: Always filter and sanitize your data.

Jonathan Sampson
I couldnt get that to work any other ideas?
A: 

Your part:

$result = mysql_connect("localhost", "******", "*****") or die ("Could not save image name

Error: " . mysql_error());

mysql_select_db("project") or die("Could not select database");
mysql_query("INSERT into dbProfiles (photo) VALUES('".$_FILES['filep']['name']."')");
if($result) { echo "Image name saved into database

";

Doesn't make much sense, your connection shouldn't be named $result but that is a naming issue not a coding one.

What is a coding issue is if($result), your saying if you can connect to the database regardless of the insert query failing or succeeding you will output "Image saved into database".

Try adding do

$realresult = mysql_query("INSERT into dbProfiles (photo) VALUES('".$_FILES['filep']['name']."')");

and change the if($result) to $realresult

I suspect your query is failing, perhaps you have additional columns or something?

Try copy/pasting your query, replacing the ".$_FILES['filep']['name']." with test and running it in your query browser and see if it goes in.

savageguy
I couldnt get that to work any other ideas?
A: 

Hi I couldn't get that to work guys sorry. Any other ideas or and idea how to do it differently, I would be very greatful.

This is my code now:

<?
$nameMember = $_POST["nameMember"];
$bandMember = $_POST["bandMember"];
$aboutMember = $_POST["aboutMember"];
$otherBands = $_POST["otherBands"];


if ($_POST["action"] == "Load")
{
$folder = "images/";

move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".$_FILES["filep"]["name"]);

echo "
<p align=center>File ".$_FILES["filep"]["name"]."loaded...";

$result = mysql_connect("localhost", "******", "*******") or die ("Could not save image name

Error: " . mysql_error());

mysql_select_db("project") or die("Could not select database");
$realresult = mysql_query(("INSERT INTO dbProfiles (nameMember,bandMember,photo,aboutMember,otherBands)
             VALUES('{$nameMember}','{$bandMember}','{$filename}','{$aboutMember}','{$otherBands}')")

$realresult { echo "Image name saved into database

"; }

}

?>

And the form I used is here:

  <form method="post" action="addMember.php" enctype="multipart/form-data">
    <p>
              Please Enter the Band Members Name.
            </p>
            <p>
              Band Member or Affiliates Name:
            </p>
            <input type="text" name="nameMember"/>
            <p>
              Please Enter the Band Members Position. Example:Drums.
            </p>
            <p>
              Member's Position:
            </p>
            <input type="text" name="bandMember"/>
            <p>
              Please Upload a Photo in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten!
            </p>
            <p>
              Photo:
            </p>
            <input type="file" name="filep" size=35 />
            <p>
              Please Enter any other information about the band member here.
            </p>
            <p>
              Other Member Information:
            </p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
            <p>
              Please Enter any other Bands the Member has been in.
            </p>
            <p>
              Other Bands:
            </p>
            <input type="text" name="otherBands" size=30 />
            <br/>
            <br/>
            <input TYPE="submit" title="Add data to the Database" value="Add Member"/>
          </form>
+1  A: 

Here is the answer for those of you looking like I did all over the web trying to find out how to do this task. Uploading a photo to a server with the file name stored in a mysql database and other form data you want in your Database. Please let me know if it helped.

Firstly the form you need:

    <form method="post" action="addMember.php" enctype="multipart/form-data">
    <p>
              Please Enter the Band Members Name.
            </p>
            <p>
              Band Member or Affiliates Name:
            </p>
            <input type="text" name="nameMember"/>
            <p>
              Please Enter the Band Members Position. Example:Drums.
            </p>
            <p>
              Band Position:
            </p>
            <input type="text" name="bandMember"/>
            <p>
              Please Upload a Photo of the Member in gif or jpeg format. The file name should be named after the Members name. If the same file name is uploaded twice it will be overwritten! Maxium size of File is 35kb.
            </p>
            <p>
              Photo:
            </p>
            <input type="hidden" name="size" value="350000">
            <input type="file" name="photo"> 
            <p>
              Please Enter any other information about the band member here.
            </p>
            <p>
              Other Member Information:
            </p>
<textarea rows="10" cols="35" name="aboutMember">
</textarea>
            <p>
              Please Enter any other Bands the Member has been in.
            </p>
            <p>
              Other Bands:
            </p>
            <input type="text" name="otherBands" size=30 />
            <br/>
            <br/>
            <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Member"/>
          </form>

Then this code processes you data from the form:

   <?php

//This is the directory where images will be saved
$target = "your directory";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name=$_POST['nameMember'];
$bandMember=$_POST['bandMember'];
$pic=($_FILES['photo']['name']);
$about=$_POST['aboutMember'];
$bands=$_POST['otherBands'];


// Connects to your Database
mysql_connect("yourhost", "username", "password") or die(mysql_error()) ;
mysql_select_db("dbName") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO tableName (nameMember,bandMember,photo,aboutMember,otherBands)
VALUES ('$name', '$bandMember', '$pic', '$about', '$bands')") ;

//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.";
}
?>

Code edited from www.about.com

Cool Hand Luke UK
A: 

i need to do something similar .Registered members for my website should be able to upload as many images with the option to delete and one of the pics uploaded must go int to thier profile as avatar.I will appreciate if anyone can help me out with it

Dabba
A: 
arjun
A: 

Adding the following avoids problems with quotes in file names, e.g.

"freddy's pic.jpg"

which are acceptable on some operating systems.

Before:

$pic=($_FILES['photo']['name']);

After:

$pic=(mysql_real_escape_string($_FILES['photo']['name']));
pog
A: 

hello i have a problem with PHP : Parse error: syntax error, unexpected T_VARIABLE in /home1/bozitask/public_html/vystava/addMember.php on line 23

my code :

? $nameMember = $_POST["nameMember"]; $bandMember = $_POST["bandMember"]; $aboutMember = $_POST["aboutMember"]; $otherBands = $_POST["otherBands"];

if ($_POST["action"] == "Load") { $folder = "images/";

move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".$_FILES["filep"]["name"]);

echo " File ".$_FILES["filep"]["name"]."loaded...";

$realresult = mysql_connect("localhost", "bozitask_vystava", "nr380ct") or die ("Could not save image name Error: " . mysql_error());

mysql_select_db("bozitask_vystava") or die("Could not select database"); $realresult = mysql_query(("INSERT INTO dbProfiles (nameMember,bandMember,photo,aboutMember,otherBands) VALUES('{$nameMember}','{$bandMember}','{$filename}','{$aboutMember}','{$otherBands}')")

$realresult { echo "Image name saved into database"; }

}

?>

rover218
A: 

Finally an answer. Why is it so hard to find this answer to such a common question? That was ridiculous! Thanks goes to Cool Hand Luke UK with his modified code.

    <?php
//This is the directory where images will be saved
$target = "images/logos/";
$target = $target . basename( $_FILES['sitelogo']['name']);

//This gets all the other information from the form
$sitelogo=($_FILES['sitelogo']['name']);


//Writes the information to the database
mysql_query("INSERT INTO pages (sitelogo) VALUES ('$sitelogo')");

//Writes the photo to the server
if(move_uploaded_file($_FILES['sitelogo']['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.";
}
?>

So here's the deal with my code. I have a form field with the name "sitelogo" so it looks like name="sitelogo" right? So this php code is on the page that processes it into the database called "insert.php". I also have my image folder located in a weird spot. So say your site is www.mysiteisawesome.com . The path to the image folder called "logos" would be "images/logos'" because it's absolute path is http://www.mysiteisawesome.com/images/logos/ . If your images folder is at www.mysiteisawesome.com/images/ you would just put "images/"; there for the $target = "images/logos/";