图像上传到文件夹但不上传到SQL数据库

时间:2017-02-17 01:25:45

标签: php mysql

我在这里是一个初学者,我想将几​​个图像上传到SQL数据库和一个文件夹,我目前正在我的本地主机上测试它,它正常上传到我的文件夹但是没有上传到SQL数据库,有什么建议吗?

HTML

<!DOCTYPE html>
<?php 
$conn = mysql_connect("localhost","root","")or die("could not connect"); 
mysql_selectdb("decorydata", $conn);
?>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>Import data from Decory DB</title>
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta name="title" content="Hemant Vishwakarma">
  <meta name="description" content="Import Excel File To MySql Database Using php">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
 </head>
 <body>    
<br><br>
    <div class="container">
        <div class="row">
         <div class="col-md-12 text-center"><h1> All Decory images </h1>    </div>
<br>
            <div class="col-md-3 hidden-phone"></div>
            <div class="col-md-6" id="form-login">
                <form class="well" action="import-img.php" method="post" enctype="multipart/form-data">
                    <fieldset>
                        <legend>Import image file</legend>
                        <div class="control-group">
                            <div class="control-label">
                                <label>image File:</label>
                            </div>
                            <div class="controls form-group">
                                <input type="file" name="files[]" id="file" class="input-large form-control" multiple>
                            </div>
                        </div>

                        <div class="control-group">
                            <div class="controls">
                            <button type="submit" id="submit" name="submit" class="btn btn-success btn-flat btn-lg pull-right button-loading" data-loading-text="Loading...">Upload images</button>
                            </div>
                        </div>
                    </fieldset>
                </form>
            </div>
            <div class="col-md-3 hidden-phone"></div>
        </div>


        <table class="table table-bordered">
            <thead>
                    <tr>
                        <th>ID</th>
                        <th>name</th>
                        <th>type</th>

                    </tr> 
                  </thead>
            <?php
                $SQLSELECT = "SELECT * FROM images";
                $result_set =  mysql_query($SQLSELECT, $conn);
                while($row = mysql_fetch_array($result_set))
                {
                ?>
                    <tr>
                        <td><?php echo $row['ID']; ?></td>
                        <td><?php echo $row['name']; ?></td>
                        <td><?php echo $row['type']; ?></td>
                    </tr>
                <?php
                }
            ?>
        </table>
    </div>
 </body>
</html>

PHP:

<?php

$conn = mysql_connect("localhost", "root", "") or die("could not connect");
mysql_selectdb("decorydata", $conn);

$rd = rand();
if (isset($_FILES['files'])) {
$errors = array();
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
    $file_name = $key . $rd . $_FILES['files']['name'][$key];
    $file_size = $_FILES['files']['size'][$key];
    $file_tmp = $_FILES['files']['tmp_name'][$key];
    $file_type = $_FILES['files']['type'][$key];
    if ($file_size > 2097152) {
        $errors[] = 'File size must be less than 2 MB';
    }
    $query = "INSERT INTO images (name,type) VALUES($file_name','$file_type',); ";
    $desired_dir = "uploadphotos";
    if (empty($errors) == true) {
        if (is_dir($desired_dir) == false) {
            mkdir("$desired_dir", 0700);  // Create directory if it does not exist
        }
        if (is_dir("$desired_dir/" . $file_name) == false) {


            $src = imagecreatefromjpeg($tmp_name);


            list($width, $height) = getimagesize($tmp_name);


            $newwidth = ($width / $height) * 150;
            $newheight = 150;
            $tmp = imagecreatetruecolor($newwidth, $newheight);

            imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
            $rd = rand();

            $filename = "thumbphotos/" . $file_name;
            imagejpeg($tmp, $filename, 100);

            imagedestroy($src);

            move_uploaded_file($file_tmp, "$desired_dir/" . $file_name);
        } else {         // rename the file if another one exist
            $new_dir = "$desired_dir/" . $file_name . time();
            rename($file_tmp, $new_dir);
        }
        mysql_query($query);
    } else {
        print_r($errors);
    }
}
if (empty($error)) {
    echo " <div class='alert alert-success'>Your Photos Is Successfully Uploded.<a href='imagesupload.php'> Add new Photos</a></div>";
}
}
?>

我错过了什么?

2 个答案:

答案 0 :(得分:0)

您是否有将数据上传到数据库的重要原因?效率不高,处理费用也很高。您必须解构图像才能将其插入数据库中。然后,当您需要将图像拉出来显示时,您将不得不重建它。此外,在数据库中拥有图像将使您的数据库大小显着增长,即使表中只有几行。 最佳做法是将图像文件上传到您已经工作的文件夹中。然后只需获取图像文件的名称及其扩展名,并将其存储在数据库中。当您需要显示图像时,只需要存储图像的静态路径,并从数据库中动态提取所需图像的名称。

答案 1 :(得分:-1)

其错字在$query字符串中。一个,;太多了。

请使用:

mysql_query($query) or die('Error: '.mysql_error());

这会向您显示错误。

请查看Mysqli而不是mysql。 http://php.net/manual/en/book.mysqli.php

相关问题