Jump to content

need help with image path


Alidad

Recommended Posts

Hi, i have wrote forms that allow me to upload images and save into the folder and send image file path name to the database, i have run it and test is works fine when save images into the folder but is not sending data to the mysql database. i'm not sure what went wrong but the result is not telling us the errors. it said it has send to database but the truth is not sending to database.

 

This is mysql table name

 

please create folder name images

 

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

 

this index.php form page

 

<form enctype="multipart/form-data" action="add.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>

 

this is retrieve information from index.php page

 

<?php 

//This is the directory where images will be saved 
$target = "images/"; 
$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("your.hostaddress.com", "username", "password") or die(mysql_error()) ; 
mysql_select_db("Database_Name") 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."; 
} 
?> 

 

and this final step to view it.

 

<?php 
// Connects to your Database 
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error()) ; 
mysql_select_db("Database_Name") 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=http://www.yoursite.com/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>"; 
}
?> 

 

 

please help thanks.

 

AM

Link to comment
Share on other sites

Hi,

 

Is it adding any data to the database (name, email etc.)?

 

You should add some error checking to your code after a db query:

mysql_query("INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')") or die("Error with insert.<br>".mysql_error());

To be able to further debug your code you need to make a variable of the insert :

$insert="INSERT INTO `employees` VALUES ('$name', '$email', '$phone', '$pic')";
mysql_query($insert)  or die("Error with insert.<br>".mysql_error());

 

This way you can easily do echo $insert to check the insert data.

 

Also, why do you have brackets in this line?:

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

They might be messing things up.

 

Chris

 

 

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.