我写了一个脚本来上传图片到一个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