2012-07-07 183 views
0

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

+2

请不要使用'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

+0

感谢提供信息 – kara 2012-07-07 19:48:21

回答

0

这条线:

mysql_query("INSERT INTO `employees` VALUES ('$images')"); 

很可能无法做你的期望。我相信你会看到插入到表格中的单词Array。您将需要插入要在阵列的每个特定部分,如:

INSERT INTO `employees` VALUES('{$images['name']}'); 

多少列请问employees表包含哪些内容?如果它有多个,则查询可能失败并显示无效的列计数消息。指定该查询到一个变量的结果,并检查错误($res = mysql_query(...); if (!$res) die(mysql_error());

从那里开始,希望帮助。随意要求进一步澄清或问题。

+0

感谢drew010对y我们的回复。它只有一列叫做图像。 – kara 2012-07-07 20:18:19

+0

好的,在这种情况下,浏览mysql表来查看它包含的内容,我想你只需要一个条目列表,因为试图将一个数组作为一个字符串使用,所有条目都会说'Array'。在PHP中,当您尝试使用数组作为字符串时,它只是返回单词'Array'。 – drew010 2012-07-07 20:19:39

+0

无法修复它.. Sh ..t http://pastebin.com/7938yQya – kara 2012-07-07 21:03:17