Help! Photo database thing again.

penguinmama

12.***.***.***
1,951 days ago

Help! Photo database thing again.

Ok, I found some code that I could tweak to upload the photos. "out of the box" it worked fine. After small tweaking it still worked... now it doesn't. anyone have any ideas?

PHP code:

<?
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['pic_file']))
$error["pic_file"] = "An image was not found.";


$imagename = basename($_FILES['pic_file']['name']);
$imagesize = basename($_FILES['pic_file']['size']);
$imagetype = basename($_FILES['pic_file']['type']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "../housepics/".$imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['pic_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
require("/usr/include/web/db.inc");
@ $db = mysql_pconnect($host, $user, $pw);
if (!$db)
{
echo "Error! Could not connect to database. Please try again later.";
echo "<a href=editproperty.php>Return to Edit Interface.</a>";
exit;
}

mysql_select_db("dbname");
$id="'".$_POST['id']."'";
$description="'".$_POST['description']."'";
$newimage="'http://dbdomain.com/housepics/".$imagename."'";
$imagesize="'".$imagesize."'";
$imagetype="'".$imagetype."'";

$query = "INSERT INTO images (id, description, filename, filesize, filetype) VALUES($id, $description, $imagename, $imagesize, $imagetype)";
$result = mysql_query($query) or die(mysql_error());
if (!$result)
{
echo "Error! Could not insert property. Call the webmaster.";
echo "<a href=editproperty.php>Return to Edit Interface.</a>";
exit;
}
echo "<h1>Your information has been inserted into the database.</h1>";
echo "The following is what you entered: ";
echo "ID: $id<br><br>Description: $description<br><br>";
echo "<img src=".$newimage.">";

echo "<a href=editproperty.php>Return to Edit Interface.</a>";
}

}

?>


<form method="POST" enctype="multipart/form-data" name="image_upload_form" action="<?$_SERVER["PHP_SELF"];?>">
<p><input type="file" name="pic_file" size="30"></p>
<p>MLS/ID# this picture goes with: <input type="text" name="id" size="10"></p>
<p>Short Description of the picture: <input type="text" name="description" size="30"></p>
<p><input type="submit" value="Upload Image" name="action"></p>
</form>

<?
if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}
?>



It's giving me an error "Unknown table 'filename' in field list" -- I do not understand WHERE it is getting the filename as a table name. I'm very confused. I'm sure (or hoping!) that it is something simple. Help?

Bill

80.***.***.***
1,951 days ago

PHP code:

$query = "INSERT INTO images (id, description, filename, filesize, filetype) VALUES($id, $description, $imagename, $imagesize, $imagetype)";


You need to edit the MySQL table in question ("images") so that the column names matches with what your query is saying.

penguinmama

12.***.***.***
1,951 days ago
See, that's just it, though. They DO.

To clarify - say I put in that I'm uploading "house1.jpg" and I put in an MLS number and a short description.

The error will say "Unknown table 'house1' in field list"

Do you see anywhere that it would be getting that as a TABLE name instead of the FILE name?

Bill

80.***.***.***
1,951 days ago
Edit: I found the error.

Change

PHP code:

$newimage="'http://dbdomain.com/housepics/".$imagename."'";



into

PHP code:

$imagename="'http://dbdomain.com/housepics/".$imagename."'";



The reason it was acting up was that the old $imagename was unenclosed and where you intended to enclose it you used an irrelevant (to the query) variable name.

penguinmama

12.***.***.***
1,950 days ago
Thank you!! That was it!! You've been a great help, THANK YOU!