【问题标题】:PHP MySQL File upload script not workingPHP MySQL文件上传脚本不起作用
【发布时间】:2012-10-18 22:57:18
【问题描述】:

我在一个网站上使用以下脚本,它运行良好 - 在此处询问后。我现在试图把它放到另一个站点,它给了我错误 Mysql error: No database selected any ideas?非常感谢您的帮助我对这些东西很陌生。从安全的角度来看,我知道这个脚本并不是最好的——我正在努力解决这个问题!

<?php 

 require_once('../Connections/BrightLights.php');

 //This is the directory where images will be saved 
 $target = "images/"; 
 $target = $target . basename( $_FILES['photo']['name']); 

 //This gets all the other information from the form 
 $name=$_POST['name']; 
 $caption=$_POST['caption']; 
 $live=$_POST['live']; 
 $photo=($_FILES['photo']['name']); 




 //Writes the information to the database 
    mysql_query("INSERT INTO `gallery` VALUES ('', '$name', '$caption', '$photo', '$live')") ;  
    if( mysql_errno() != 0){
     // mysql error
     // note: message like this should never appear to user, should be only stored in log
     echo "Mysql error: " . htmlspecialchars( mysql_error());
     die();
}

 //Writes the photo to the server 
 if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
 { 

 //Tells you if its all ok 
 echo "The file ". basename( $_FILES['uploadedfile']['name']). " 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 mysql file-upload


【解决方案1】:

当您忘记选择带有mysql_select_db() 并调用mysql_query() 的数据库时,通常会出现“未选择数据库”错误

【讨论】:

  • 好的,感谢您的评论,我将如何在我的上述代码中实现它。我在另一个网站上使用了相同的代码,它工作正常。
  • 我需要查看BrightLights.php 文件中的代码,以检查是什么使它在一个站点上运行而不是在另一个站点上运行。要使您当前的代码正常工作,您只需在 require 后添加 mysql_select_db('your_database_name');
  • 非常感谢您的帮助,我添加了 mysql_select_db('your_database_name');现在可以完美运行了。
  • 如果您问我如何实现它,只是想知道您为什么选择其他答案?我觉得不合逻辑?
  • 抱歉,我以为我两个都选了,但事实证明你不能这样做! - 我现在改了 :-)
【解决方案2】:

在查询数据库之前,必须先连接并选择数据库,然后才能查询。使用这个:

mysql_connect(hostname, username, password) or die(mysql_error());
mysql_select_db(database_name);

【讨论】:

  • 非常感谢您的帮助! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多