Jump to content

image and form data fail to be inserted into database

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
fredrick

fredrick

    Newbie

  • Members
  • Pip
  • 2 posts
Hi every one, i try to upload form data into database along with the image but the image name will be in my database as link while the image itself will be in a directory. I have use something like this code before it works i think but the problem with this code is what i dont know.Is it the directory as it keeps on returning the error

-------Sorry, there was a problem uploading your file------
the code is as follow

------------------------image.php--------------------------

<form enctype="multipart/form-data" action="addimage.php" method="POST"> 

 Name: <input type="text" name="name"><br> 

 E-mail: <input type="text" name = "email"><br> 

 Phone: <input type="text" name = "phone"><br> 

 Photo: <input type="file" name="photo"><br> 

 <input type="submit" value="Add"> 

 </form>



-----------------------------addimage.php---------------

<?php 

 

 //This is the directory where images will be saved 

 $target = "C:/Tomcat 6.0/webapps/fphp/image/"; 

 $target = $target . basename( $_FILES['photo']['name']); 

 

 //This gets all the other information from the form 

 $name=$_POST['name']; 

 $email=$_POST['email']; 

 $phone=$_POST['phone']; 

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

 

 // Connects to your Database 

 mysql_connect("localhost", "root", "root") or die(mysql_error()) ; 

 mysql_select_db("student") or die(mysql_error()) ; 

 

 //Writes the information to the database 

 mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") ; 

 

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

 } 

 ?> 





the table is as follow


CREATE TABLE employees (name VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), photo text);



now to view the image i develope

<?php 

 // Connects to your Database 

 mysql_connect("localhost", "root", "root") or die(mysql_error()) ; 

 mysql_select_db("student") or die(mysql_error()) ; 

 

 //Retrieves data from MySQL 

 $data = mysql_query("SELECT * FROM employees") or die(mysql_error()); 

 //Puts it into an array 

 while($info = mysql_fetch_array( $data )) 

 { 

 

 //Outputs the image and other data

 Echo "<img src="C:/Tomcat 6.0/webapps/fphp/images/".$info['photo'] ."> <br>"; 

 Echo "<b>Name:</b> ".$info['name'] . "<br> "; 

 Echo "<b>Email:</b> ".$info['email'] . " <br>"; 

 Echo "<b>Phone:</b> ".$info['phone'] . " <hr>"; 

 }

 ?> 





thanks and i will be glad to hear from you.

#2
johnny123

johnny123

    Newbie

  • Members
  • PipPip
  • 10 posts
Are you sure your permissions to upload files on the server are correct? To me it looks like the permission upload issue on the server.

#3
fredrick

fredrick

    Newbie

  • Members
  • Pip
  • 2 posts
Thank you for your reply .if permission was the case how do i solve it as am running the programme on my local server window xp.how will know that i have permission to upload on my localhost.if then how will i set the permission. thanks and God bless.