使用pdo保存图像链接到数据库时出错

时间:2014-06-21 22:04:51

标签: php html ajax file-upload pdo

我想做的是制作一个图片上传系统并将图片发送到upload文件夹并将链接保存到数据库。

我的问题是我的错误images.php我可以帮助我。

enter image description here

HTML:

<form method="post" enctype="multipart/form-data">  
<img id="picture" data-src="#" /> <br />
<input type='file' name="image" id="imgInp" accept="image/*" /><br />
<input type="submit" name="submit" id="submit" value="submit" />
</form>

脚本:

<script type="text/javascript">
$(document).ready(function() {

   $('#submit').click(function (e) {
       e.preventDefault();

        var data = {};

        data.image = $('#imgInp').val();


        $.ajax({
            type: "POST",
            url: "images.php",
            data: data,
            cache: false,
            success: function (response) {


            }
        });
            return false;
    });

});
</script>

images.php

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";

$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$image = addslashes(file_get_contents(@$_FILES['image']['tmp_name']));
$image_name = addslashes(@$_FILES['image']['name']);
$image_size = getimagesize(@$_FILES['image']['tmp_name']);

move_uploaded_file(@$_FILES["image"]["tmp_name"], "upload/" . @$_FILES["image"]["name"]);
$location = "upload/" . @$_FILES["image"]["name"];

$q = "INSERT INTO students( image ) VALUES( :image)";

$query = $dbc->prepare($q);
$query->bindParam(':image', $location);


$results = $query->execute();

?>
用于图片上传的

脚本:

<script type="text/javascript">
$(document).ready(function() {
    var currentSrc = $('#picture').attr('src');
    if(currentSrc==null || currentSrc==""){        
    $('#picture').attr('src','http://i38.photobucket.com/albums/e149/eloginko/profile_male_large_zpseedb2954.jpg');

   $("#picture").on('click', function() {
       $("#imgInp").trigger('click')}
   )}


    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();

            reader.onload = function (e) {
                $('#picture').attr('src', e.target.result);
            }

            reader.readAsDataURL(input.files[0]);
        }
    }

    $("#imgInp").change(function(){
        readURL(this);
    });

});
</script>

2 个答案:

答案 0 :(得分:0)

摆脱错误消息最简单的方法是实际放置一个条件,检查$_FILES中是否有任何内容。但过去那个,不清楚$FILES空洞的根本原因。根据我的经验,在另一方面使用PHP接收器上传Ajax文件时,最好不能始终如一地工作。无论如何,这是我的代码版本,有条件的地方:

if (!empty($_FILES)) {

    $image = addslashes(file_get_contents(@$_FILES['image']['tmp_name']));
    $image_name = addslashes(@$_FILES['image']['name']);
    $image_size = getimagesize(@$_FILES['image']['tmp_name']);

    move_uploaded_file(@$_FILES["image"]["tmp_name"], "upload/" . @$_FILES["image"]["name"]);
    $location = "upload/" . @$_FILES["image"]["name"];

    $q = "INSERT INTO students( image ) VALUES( :image)";

    $query = $dbc->prepare($q);
    $query->bindParam(':image', $location);


    $results = $query->execute();

}

答案 1 :(得分:0)

尝试这种方法,if语句可能看起来太多了,但如果你想要可靠的代码,你需要检查:

if(is_uploaded_file($_FILES['image']['tmp_name'])){ 
    $folder = "upload/"; 
    $file = basename( $_FILES['image']['name']); 
    $full_path = $folder.$file; 
    if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) { 
        echo "succesful upload, we have an image!";
        //PDO
        $dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
        $dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $query = "INSERT INTO students( image ) VALUES( :image)";
        $stmt = $dbc->prepare($query);
        $stmt->bindParam(':image', $full_path);
        $results = $stmt->execute();
        if($results){
            echo "Insert successful!";
        }else{
             echo "Insert failed!";
        }
    } else { 
       echo "upload received! but process failed";
    } 
}else{ 
    echo "upload failure ! Nothing was uploaded";
} 

我想指出几点:

  • @抑制错误,即使在进行故障排除时也不希望出现这种情况 生产,你仍然想知道错误。
  • 您应启用错误报告
  • 您没有检查php脚本是否收到图片上传。
  • 在这种情况下,file_get_contents的使用尚不清楚
  • 您没有使用$image*变量? ....