【问题标题】:Having difficulties Uploading Photo in Database through PHP通过 PHP 在数据库中上传照片时遇到困难
【发布时间】:2013-07-28 21:44:15
【问题描述】:

需要帮助: 我正在使用一个简单的 PHP 代码将照片上传到远程数据库。但是.. 每次,一张照片的两个副本都保存在数据库中。 谁能告诉我我做错了什么? PHP代码:

<?PHP
$uploadDir = 'image_folder/'; 



$uploadDir = 'image_folder/'; 

if(isset($_POST['Submit']))  //info saving in the variables
{
$fileName = $_FILES['Photo']['name'];
$tmpName  = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath); //moving the photo in the         destination
if (!$result) {
echo "Error uploading file";
exit;
}
if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
    $filePath = addslashes($filePath);
}
echo "".$filePath."";
$query = "INSERT INTO picture (image) VALUES ('$filePath')";
if (mysql_query($query))
{echo "Inserted";}
mysql_query($query) or die('Error loading file!'); 
}?>

【问题讨论】:

    标签: php mysql database upload photo


    【解决方案1】:
    if (mysql_query($query))
    {echo "Inserted";}
    mysql_query($query) or die('Error loading file!'); 
    

    你打电话给mysql_query($query) 两次

    【讨论】:

    • 嘿,非常感谢! :)
    【解决方案2】:

    你做了两次mysql_query($query),第一次是IF{},然后是它。 :D

    ps。 mysql_* 功能已废弃,请使用PDOmysqli

    【讨论】:

    【解决方案3】:

    您使用了两次mysql_query。试试这个:

    if (mysql_query($query)) {
      echo "Inserted";
    } else {
     die('Error loading file!'); 
    }
    

    【讨论】:

      【解决方案4】:

      您正在执行 mysql_query 两次。将您的 if 修改为:

        if (mysql_query($query)) {
              echo "Inserted";
        } else {
           die('Error loading file!')
        }
      

      然后,删除以下行:

       mysql_query($query) or die('Error loading file!'); 
      

      注意:确保您阅读了http://in2.php.net/mysql_query 中的警告框。 mysql_* 函数已弃用。

      【讨论】:

        猜你喜欢
        • 2013-10-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-21
        • 1970-01-01
        • 1970-01-01
        • 2013-01-23
        • 1970-01-01
        相关资源
        最近更新 更多