Jump to content

Recommended Posts

Hi all

I am still having trouble with this form, I am looking to allow users to upload a picture to bd, and I'm looking to save that image to a folder (../Database/pics) and then insert the name of the image in a field in my db, I can successfully get the user to upload a picture but it stores their local path to the picture in the DB, I'm not sure how I can get around this any suggestions I have looked every where but can find any similar examples

The code I'm using is:

Form code:
[code]
<html>
<head>
  <title>form.php</title>
  <link rel="stylesheet" href="etc/TJConsultants.css">
</head>
<body>
<br>
<div id="wrapper">
<div id="header">
<div style="text-align: center;"><br>
</div>

<br>

</div>


<div id="centercolumn">
<div style="text-align: center;"><br>

</div>
<div style="text-align: center;">
<hr style="width: 100%; height: 2px;"><font style="color: rgb(21, 27, 84);" size="+3">New
Employee Entry Form<br>

</font>
<hr style="width: 100%; height: 2px;"><font style="color: rgb(21, 27, 84);" size="+3"><br>
</font>

<form method="post" action="script_1.php"><br>

 
  <table style="text-align: left; margin-left: auto; margin-right: auto; width: 490px; height: 495px;" border="1" cellpadding="2" cellspacing="2">

    <tbody>

      <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Surname:</td>
        <td><input name="Surname" size="40" type="text"></td>
      </tr>
      <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Forename:</td>
        <td><input name="Forename" size="40" type="text"></td
      </tr>
      <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Job Title:</td>
        <td><input name="Job_title" size="40" type="text"></td>
      </tr>
      <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);">DOB:</td>
        <td><input name="DOB" size="40" type="text"></td>
      </tr>
      <tr>
      <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Telephone:</td>
        <td><input name="Telephone" size="40" type="text"></td>
      </tr>
      <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Email:</td>

        <td><input name="Email" size="40" type="text"></td>
      </tr>
      <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Office Location:</td>
        <td><input name="Office_location" size="40" type="text"></td>
      </tr>
      <tr>
      <td style="text-align: left; font-family: Tahoma; color: rgb(0, 0, 102);">Expertise:</td>
        <td><textarea rows="2" name="Expertise" cols="35"></textarea></td>
      </tr>
    <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);">Hobbies:</td>
        <td><textarea rows="2" name="Hobbies" cols="35"></textarea></td>
      </tr>
      <tr>
        <td style="font-family: Tahoma; color: rgb(0, 0, 102);"><br>
Picture:<br>
</td>
        <td><input name="Picture" size="35" type="file"></td>
      </tr>
   
    </tbody>
 
  </table>
  <br>

  <input value="Enter Record" type="submit">
</form>

<br>

<br>

<hr style="width: 100%; height: 2px;">
<div style="text-align: left;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 46px;" alt="Home" src="etc/Home_button.JPG"></a><br>
</div>

<hr style="width: 100%; height: 2px;">
</div>


</div>


<div id="footer">
<p style="text-align: left;"></p>


</div>


</div>


</body>
</html>

[/code]

My script code is:

[code]
<html>
<head>
  <title>Employee Index</title>
</head>
<body>

<h1 style="text-align: center;"><img src="etc/Logo_pic_1.JPG"></h1>
<hr style="width: 100%; height: 2px;">

<?php $user="root";
$host="localhost";
$password="";
$database = "employee_index";
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("employee_index") or die(mysql_error());

$sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture)
VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]','$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$_POST[Picture]')";

$result = mysql_query($sqlquery)
or die (mysql_error());

mysql_close();

print "<html><body><center>";
print "<p><B>Thank You - You have just entered the following information successfully!</B><p>";
print "Surname : $_POST[Surname]<br>";
print "Forename : $_POST[Forename]<br>";
print "Job Titile : $_POST[Job_title] <br>";
print "Office Location : $_POST[Job_title]<br>";
print "Telephone : $_POST[Telephone] <br>";
print "Email :$_POST[Telephone] <br>";
print "Expertise : $_POST[Expertise]<br>";
print "Hobbies : $_POST[Hobbies] <br>";
print "DOB : $_POST[DOB]<br>";
print "Picture File: : $_POST[Picture]<br>";
print "</body></html>";
?>

<hr style="width: 100%; height: 2px;">
<p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p>
<hr style="width: 100%; height: 2px;">



</body>
</html>

[/code]

Any advice would be welcomed

Thanks

Jackie
Link to comment
https://forums.phpfreaks.com/topic/34904-problems-uploading-pics-from-form/
Share on other sites

You're missing a whole bunch of code relative to the uploading and moving of the image.

[code]<?php
$target_path = "../Database/pics";

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

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

This moves the temporary image location (which is what you're getting) into a permanent location into your /pics folder.

This does not provide any validation of the image or its extension. You should include that as well or someone could upload something ugly to your site.
Hi there

Thanks for your help, I have added the suggested code and it saves a copy of the image to the directed file perfectly, however it is still not saving the file name in the db it just say array, not to sure why, any suggestions with this?

Copy of my code now:
[code]<html>
<head>
  <title>Employee Index</title>
</head>
<body>

<h1 style="text-align: center;"><img src="etc/Logo_pic_1.JPG"></h1>
<hr style="width: 100%; height: 2px;">

<?php $user="root";
$host="localhost";
$password="";
$database = "employee_index";
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("employee_index") or die(mysql_error());

$sqlquery = "INSERT INTO employee (Surname, Forename, Job_title, Office_location, Telephone, Email, Expertise, Hobbies, DOB, Picture)
VALUES ('$_POST[Surname]','$_POST[Forename]','$_POST[Job_title]','$_POST[Office_location]','$_POST[Telephone]','$_POST[Email]','$_POST[Expertise]','$_POST[Hobbies]','$_POST[DOB]','$_FILES[Picture] ['name']')";

$result = mysql_query($sqlquery)
or die (mysql_error());

mysql_close();

print "<html><body><center>";
print "<p><B>Thank You - You have just entered the following information successfully!</B><p>";
print "Surname : $_POST[Surname]<br>";
print "Forename : $_POST[Forename]<br>";
print "Job Titile : $_POST[Job_title] <br>";
print "Office Location : $_POST[Job_title]<br>";
print "Telephone : $_POST[Telephone] <br>";
print "Email :$_POST[Telephone] <br>";
print "Expertise : $_POST[Expertise]<br>";
print "Hobbies : $_POST[Hobbies] <br>";
print "DOB : $_POST[DOB]<br>";
print "Picture File: : $_FILES[Picture]<br>";
print "</body></html>";


$target_path = "../test_3/pics";

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

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

?>

<hr style="width: 100%; height: 2px;">
<p style="text-align: center;"><a href="index.html"><img style="border: 0px solid ; width: 94px; height: 47px;" alt="Home" src="etc/Home_button.JPG"></a></p>
<hr style="width: 100%; height: 2px;">



</body>
</html>

[/code]


Regards

Jackie
There's a couple of things to do in order to get the actual name into the database. This should work:

add this line into your code:

[code]$imagename = basename($_FILES['Picture']['name']);[/code]

Then in your INSERT query change the reference for the picture to simply: '$imagename'

Basically what we've done is set a variable populated with the image's actual name. Then, in your query, it should use that for populating the database field. In your pages where you want the image to show you'd use a <img src tag like usual but like this:

<img src='http://www.yoursitename.com/filedir/$imagename'>
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.