I wrote a script to upload an image to a folder with PHP which should link the filename to MySQL.
I think after the long day I've missed something which I can't see :(
upload.php
<form enctype="multipart/form-data" action="add.php" method="POST">
Photo: <input type="file" name="images"><br>
<input type="submit" value="Add">
</form>
add.php
<?php
error_reporting(E_ALL);
//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['images']);
//This gets all the other information from the form
$images=($_FILES['images']);
// Connects to your Database
mysql_connect("localhost", "root", "pass") or die(mysql_error()) ;
mysql_select_db("formular") or die(mysql_error()) ;
//Writes the information to the database
mysql_query("INSERT INTO `employees` VALUES ('$images')") ;
//Writes the pictures to the server
if(move_uploaded_file($_FILES['images']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']). " 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.";
}
?>
view.php
<?php
// Connects to your Database
mysql_connect("localhost", "root", "pass") or die(mysql_error()) ;
mysql_select_db("formular") or die(mysql_error()) ;
//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM employees") or die(mysql_error());
//Puts it into an array
while($info = mysql_fetch_array( $data ))
{
//Outputs the image and other data
Echo "<img src=images/".$info['images'] ."> <br>";
}
?>
thx at all
mysql_*
functions to write new code. They are no longer maintained and the community has begun deprecation process. See the red box? Instead you should learn about prepared statements and use either PDO or MySQLi. If you can't decide which, this article will help you. If you pick PDO, here is good tutorial. – tereško Jul 7 '12 at 19:40