PHP Help Pleeeeease!

Associate
Joined
4 Dec 2006
Posts
21
Location
Newcastle [UK]
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.';
}
?>
 
firstly...let me play Rob

robmiller said:
escape your input!!
with that out the way:

all you need is another column, and to run your query after the file's uploaded, so you can use the details to submit a url. you could either submit the url, or an anchor ref (personally i'd just do the URL as it's more portable)

something like

Code:
$imageLink = "{$_SERVER['DOCUMENT_ROOT']}/images/usedcars/ {$_FILES['file']['name']}";
so together it reads
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");

?>
<?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.';
		
		//moved the mysql query here so it executes when the file's uploaded
		$imageLink = "{$_SERVER['DOCUMENT_ROOT']}/images/usedcars/ {$_FILES['file']['name']}";
		
		$sql="INSERT INTO tblUSEDCARS (fldMAKE,fldMODEL,fldENGINE,fldPRICE,fldMILEAGE,fldYEAR,fldCOLOUR,fldDESC,urlToIMG) values ('$fldMAKE','$fldMODEL','$fldENGINE','$fldPRICE','  $fldMILEAGE','$fldYEAR','$fldCOLOUR','$fldDESC','$imageLink')";
		if (mysql_query($sql,$db))
		{
        echo "Vehicle has been added to the database.<p>";
		}
		else
		{
        die(mysql_error('Failed to add vehicle'));
		}
	}
} 
else
{
	echo 'Sorry, we only accept .gif images under 5Kb for upload.';
}
?>

note that you need an extra column in your tblUSEDCARS table called urlToIMG for this to work. obviously untested

edit: vBulletin likes to insert spaces...hunt them out and delete them :)
 
Back
Top Bottom