我寫了一個腳本來上傳圖片到一個PHP文件夾,它應該鏈接文件名到MySQL。鏈接到MySQL的PHP圖片上傳
我認爲這漫長的一天,我錯過了一些東西,我看不到:(
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
請不要使用'mysql_ *'函數來編寫新代碼,它們不再被維護,並且社區已經開始[棄用過程](http://goo.gl/KJveJ)。請參閱[*紅框*](http://goo.gl/GPmFd )?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http:// php。 net/mysqli)。如果你不能決定哪些,[這篇文章](http://goo.gl/3gqF9)會幫助你。如果你選擇PDO,[這裏是很好的教程](http:// goo。 gl/vFWnC) – 2012-07-07 19:40:24
感謝提供信息 – kara 2012-07-07 19:48:21