File and info upload

Links for php scripts

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 8
Joined: Mon May 28, 2012 6:39 am

Mon May 28, 2012 6:46 am

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;
if(move_uploaded_file($_FILES['pic']['tmp_name'], $target))
echo "The file ". basename( $_FILES['pic']['name']). " has been uploaded";

// if successfully updated.
header( 'Location: entry_finder.php' ) ;
else {
echo "ERROR";


User avatar
php-forum Fan User
php-forum Fan User
Posts: 979
Joined: Thu Feb 17, 2011 6:52 am
Location: Racine, WI

Tue May 29, 2012 6:42 am

It looks like you just have some errors.

This line:

Code: Select all

$pic = ($_FILES['pic']['name']);
Should be:

Code: Select all

$pic = $_FILES['pic']['name'];

Then this line:

Code: Select all

history='$history', cond='$cond', pic'$pic' WHERE id='$id'";
Should be:

Code: Select all

history='$history', cond='$cond', pic='$pic' WHERE id='$id'";

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest