Here is my code:
form.html
<form enctype="multipart/form-data" action="add.php" method="POST"> Product Name: <input type="text" name="name" /><br /> Product Size: <input type="text" name ="size" /><br /> Product Type: <input type="text" name ="type" /><br /> Product Image: <input type="file" name="photo" /><br /> <input type="submit" value="Add" /> </form>
add.php
<?php
//This is the directory where images will be saved
$target = "/Devel/uploads/";
$target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$size=$_POST['size'];
$type=$_POST['type'];
$pic=($_FILES['photo']['name']);
// Connects to your Database
mysql_connect("xxxx", "xxxx", "xxxx") or die(mysql_error()) ;
mysql_select_db("Devel") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `products` VALUES ('$name', '$size', '$type', '$pic')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['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.";
}
?>
display.php
<?php
// Connects to your Database
mysql_connect("xxxx", "xxxx", "xxxx") or die(mysql_error()) ;
mysql_select_db("Devel") or die(mysql_error()) ;
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM products") or die(mysql_error());
echo "<table border='1'>
<tr>
<th>Product Name</th>
<th>Product Size</th>
<th>Product Type</th>
<th>Product Image</th>
</tr>";
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
echo "<tr>";
Echo "<td>".$info['name'] . "</td>";
Echo "<td>".$info['size'] . "</td>";
Echo "<td>".$info['type'] . "</td>";
Echo "<td>".$info['photo'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>

New Topic/Question
Reply



MultiQuote





|