I am trying to create a form to handle information about a machine, as well as an option for adding and uploading a photo for the description. I know this should be easy, but I am new to php scripting and with a lot of study an effort, I've gotten the script to perform all the tasks except writing the file name for the $pic variable into the mysql database. Does anyone have any advice to help me get the uploaded file name to write into the database? Below is my code:
<?php include("settings.php");
// Connect to server and select database.
mysql_connect("$hostname", "$username", "$password")or die("cannot connect");
mysql_select_db("$databasename")or die("cannot select DB");
$id = $_POST['id'];
$name = $_POST['name'];
$maker = $_POST['maker'];
$year = $_POST['year'];
$type = $_POST['type'];
$serial = $_POST['serial'];
$info = $_POST['info'];
$sale = $_POST['sale'];
$price = $_POST['price'];
$history = $_POST['history'];
$cond = $_POST['cond'];
$pic = ($_FILES['pic']['name']);
$target = "../uploads/";
$target = $target . basename( $_FILES['pic']['name']);
// update data in mysql database
$sql="UPDATE machines SET name='$name', maker='$maker', year='$year', type='$type', serial='$serial', info='$info', sale='$sale', price='$price', history='$history', cond='$cond', pic'$pic' WHERE id='$id'";
//echo $sql;
$result=mysql_query($sql);
if(move_uploaded_file($_FILES['pic']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['pic']['name']). " has been uploaded";
}
// if successfully updated.
if($result){
header( 'Location: entry_finder.php' ) ;
}
else {
echo "ERROR";
}
?>
File and info upload
Moderators: egami, macek, gesf
It looks like you just have some errors.
This line:
Should be:
Then this line:
Should be:
This line:
Code: Select all
$pic = ($_FILES['pic']['name']);
Code: Select all
$pic = $_FILES['pic']['name'];
Then this line:
Code: Select all
history='$history', cond='$cond', pic'$pic' WHERE id='$id'";
Code: Select all
history='$history', cond='$cond', pic='$pic' WHERE id='$id'";