A client was looking for a CMS system for the site that I build for him. Part of this was to give my client the ability to upload pictures along with a descriptions of the cars that they are selling. The coding that I found earlier for this would upload a picture but would not carry along a description with it as well. So I had a thought, why can’t I upload a file to a folder along with uploading the file name and a description to a database and then reflect that back to a site using PHP.
Here is what I came up with:
1. First thing you will need to do is to set up your database with the tables that will reflect the form. Next create a new page called form.html adn set up your form giving it the action “add.php”. Then you will need to set up a folder in the directory that this file will be save in either locally or on your server’s file. This code will upload the picture to the above folder.
<?php
//This is the directory where images will be saved
$target = “uploads/”;
$target = $target . basename( $_FILES['uploadedfile']['name']);
2. Next you will need the code to upload information to your database. In this example, I am using a MySQL database:
//This gets all the other information from the form
$name=$_POST['name'];
$model=$_POST['model'];
$year=$_POST['year'];
$odometer=$_POST['odometer'];
$price=$_POST['price'];
$pic=($_FILES['uploadedfile']['name']);
$description=$_POST['description'];
mysql_connect(“www.mydatabase.com, “database name”, “password”) or die(mysql_error()) ;
mysql_select_db(“database”) or die(mysql_error()) ;
//Writes the information to the database
mysql_query(“INSERT INTO `sale` VALUES (‘$name’, ‘$model’, ‘$year’, ‘$odometer’, ‘$price’, ‘$pic’, ‘$description’)”) ;
3. Now the above will file this information on your database. This code has will reflect an error if the file did not go to the file on your server. Now for the rest of the code to send the photo to the file:
//Writes the photo to the server
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target))
{
//Tells you if its all ok
echo “The file “. basename( $_FILES['uploadedfile']['name']). ” has been uploaded, and your information has been added to the directory”;
}
else {
//Gives and error if its not
echo “Sorry, there was a problem uploading your file.”;
}
?>
4. Save this file as add.php
5. Now here is the code for the display of what you just uploaded:
<?php
mysql_connect(“www.mydatabase.com, “database name”, “password”) or die(mysql_error()) ;
$data = mysql_query(“SELECT * FROM your table”) or die(mysql_error());?>
<?php $info = mysql_fetch_array( $data ); ?>
<?php do { ?>
<table width=”500″ border=”1″ cellspacing=”0″ cellpadding=”0″ align=”center”>
<tr>
<td>Photo</td>
<td>Make</td>
<td>Model</td>
<td>Year</td>
<td>Odometer</td>
<td>Price</td>
<td>Description</td>
</tr>
<tr>
<td><img src=”<?php echo “http://www.yourwebsite.com/uploads/”.$info['photo'] .”"; ?> ” alt=”" name=”" width=”100″ height=”90″ border=”1″ /></td>
<td><?php echo $info['name']; ?></td>
<td><?php echo $info['model']; ?></td>
<td><?php echo $info['year']; ?></td>
<td><?php echo $info['odometer']; ?></td>
<td><?php echo $info['price']; ?></td>
<td><?php echo $info['description']; ?></td>
</tr>
</table>
<?php } while ($info = mysql_fetch_assoc($data)); ?>
6. Save this as display.php. What I have in this coding is a repeat command that will show everything in the database so you can show all at the same time.
That should do it. Play with it and see if there are areas that can be improved upon and let me know.



Recently I had a client that need to upload a 2000 row 13 column excel spreadsheet to a MySQL database. The client is a car broker who basically helps people find specific cars and he also buys luxury cars and sells them via his website.