liamC
Member
Registered: 28th Feb 04
User status: Offline
|
OK, I have set up a PHP/MYSQL Database to add cars to a database. Each car added by the user comes with an image of said vehicle, which the user uploads from their machine along with the cars make/model/etc.
I have got the database description fields of the vehicle adding to the database 100% successfully.
I have also got the images uploading to my webspace successfully. This is also working 100%.
What I want however, is when the image is uploaded, to somehow put the address to the image into a database field alongside the rest of the row of details of the vehicle, so when the user wants to view that vehicle, I can just echo out the image URL to go alongside all of the rest of the vehicles details. [Hope this makes sense!]
I have pasted my code below to see if anyone can help out? I have already added another field to the database called fldIMG where I would like the URL of the uploaded image to go to. The below is all the code I have which successfully adds all the required info.
Any help appreciated
code: <?php
// Read values from form using $_POST (safest)
$fldMAKE=$_POST["fldMAKE"];
$fldMODEL=$_POST["fldMODEL"];
$fldENGINE=$_POST["fldENGINE"];
$fldPRICE=$_POST["fldPRICE"];
$fldMILEAGE=$_POST["fldMILEAGE"];
$fldYEAR=$_POST["fldYEAR"];
$fldCOLOUR=$_POST["fldCOLOUR"];
$fldDESC=$_POST["fldDESC"];
// Connect to server
// Replace username and password by your details
$db = @mysql_connect("dbname.domainname.com","username","password");
if (!$db)
{
do_error("Could not connect to the server");
}
// Connect to the database
// Note that your database will be called username
@mysql_select_db("usedcars",$db)or do_error("Could not connect to the database");
// Run query
$sql="INSERT INTO tblUSEDCARS (fldMAKE,fldMODEL,fldENGINE,fldPRICE,fldMILEAGE,fldYEAR,fldCOLOUR,fldDESC) values ('$fldMAKE','$fldMODEL','$fldENGINE','$fldPRICE','$fldMILEAGE','$fldYEAR','$fldCOLOUR','$fldDESC')";
if (mysql_query($sql,$db))
{
echo "Vehicle has been added to the database.<p>";
}
else
{
do_error("Failed to add record");
}
function do_error($error)
{
echo $error;
die;
}
?>
<?php
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
echo "Uploading " . $_FILES["file"]["name"];
echo " (" . $_FILES["file"]["type"] . ", ";
echo ceil($_FILES["file"]["size"] / 1024) . " Kb).<br />";
echo "File is temporarily stored as " . $_FILES["file"]["tmp_name"];
?>
<?php
if (($_FILES['file']['type'] == 'image/gif') &&
($_FILES['file']['size'] < 5000))
{
echo 'Return Code: ' . $_FILES['file']['error'] . '<br />';
echo 'Uploading ' . $_FILES['file']['name'] . ' (' .
$_FILES['file']['type'] . ', ' .
ceil($_FILES['file']['size'] / 1024) . ' Kb).<br />';
if (file_exists('../images/usedcars/' . $_FILES['file']['name']))
{
echo $_FILES['file']['name'] . ' already exists. ';
echo 'Please delete the destination file and try again.';
}
else
{
move_uploaded_file($_FILES['file']['tmp_name'],
'../images/usedcars/' . $_FILES['file']['name']);
echo 'File has been stored in your uploads directory.';
}
} else
{
echo 'Sorry, we only accept .gif images under 5Kb for upload.';
}
?>
[Edited on 02-03-2007 by liamC]
|