I am using following code to UPDATE database and upload file. It updates fields in database name,email,phone.
But query does not work for photo field. It gives error. And considers '$pic' as empty.

Help Plz.

Database

CREATE TABLE employees (name VARCHAR(30), email VARCHAR(30), phone VARCHAR(30), photo VARCHAR(30))
<?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']); 
 $id=$_POST['id'];

 // 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 
$sql="UPDATE `employees` SET name='$name',email='$email',phone='$phone',photo='$pic' WHERE id='$id' "; 
 $result=mysql_query($sql); 
 
 //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."; 
 } 
 ?>

Hi

//Writes the information to the database 
$sql="UPDATE `employees` SET name='$name',email='$email',phone='$phone',photo='$pic' WHERE id='$id' "; 
 $result=mysql_query($sql);

Please try the below one

$sql="UPDATE `employees` SET name='$name',email='$email',phone='$phone',photo='$pic' WHERE id='$id' ";
mysql_db_query($database,$sql);
	$change=$_POST["id"];

Two questions:
1) can you show the html part?
2) the script is saving the image to the path?

Thanks.

Hi

//Writes the information to the database 
$sql="UPDATE `employees` SET name='$name',email='$email',phone='$phone',photo='$pic' WHERE id='$id' "; 
 $result=mysql_query($sql);

Please try the below one

$sql="UPDATE `employees` SET name='$name',email='$email',phone='$phone',photo='$pic' WHERE id='$id' ";
mysql_db_query($database,$sql);
	$change=$_POST["id"];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.