0

我编写了一个脚本来使用 PHP 将图像上传到一个文件夹,该文件夹应该将文件名链接到 MySQL。

我想在漫长的一天之后我错过了一些我看不到的东西:(

上传.php

<form enctype="multipart/form-data" action="add.php" method="POST">
 Photo: <input type="file" name="images"><br> 
 <input type="submit" value="Add"> 
 </form>

添加.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."; 
 } 
 ?> 

视图.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>";
 }
 ?> 

完全感谢

4

1 回答 1

0

这一行:

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

可能没有按照您的预期进行。Array我相信您会看到表格中每一行插入的单词。您将需要插入所需的数组的每个特定部分,例如:

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

employees该表包含多少列?如果它有多个,则查询可能会失败,并显示无效的列计数消息。将该查询的结果分配给一个变量并检查它是否有错误($res = mysql_query(...); if (!$res) die(mysql_error());

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

于 2012-07-07T19:53:17.280 回答