提问者:小点点

如何用php,javascript,ajax,jQuery在数据库中存储文件夹数据并检索出文件夹数据?


我的代码目前正在工作上传和删除数据到一个文件夹,并可以存储在数据库内,但东西是不能删除甚至在数据库,我也困惑如何打印出上传的数据。 这是密码。 这是AddRemove.php

<?php
require_once 'config.php';
$request = $_POST['request'];

// Upload file
if($request == 1){

    $filename = $_FILES['file']['name'];
    /* Location */
    $location = "upload/".$filename;
    $uploadOk = 1;
    $imageFileType = pathinfo($location,PATHINFO_EXTENSION);

    // Check image format
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
     && $imageFileType != "gif" ) {
        $uploadOk = 0;
    }
    else
    {
        $imgContent = addslashes(file_get_contents($_FILES["file"]["tmp_name"]));
        $insert = $db->query("INSERT into images (image, uploaded) VALUES ('$imgContent', NOW())");
    }

    if($uploadOk == 0){
        echo 0;
    }else{
     /* Upload file */
        if(move_uploaded_file($_FILES['file']['tmp_name'],$location)){
            echo $location;
        }else{
            echo 0;
        }
    }
    exit;
}
//Remove file
if($request == 2){
    $path = $_POST['path'];

    $return_text = 0;

    // Check file exist or not
    if( file_exists($path) )
    {
     $db->query("DELETE FROM images WHERE id = ? LIMIT 1");
    // Remove file 
     unlink($path);
     
    // Set status
     $return_text = 1;
    }
    
    else
    {

    // Set status
     $return_text = 0;
    }

    // Return status
    echo $return_text;
    exit;
?>

这是index.html。

<!doctype html>
<html>
    <head>
        <title>Upload and delete image file with jQuery and AJAX</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link href='style.css' rel='stylesheet' type='text/css'>
        
    </head>
    <body >
        
        <form method="post" action="" enctype="multipart/form-data" id="myform">
               
            <input type="file" id="file" name="file" />
            <input type="button" class="button" value="Upload" id="but_upload">
            
        </form>
        
        <!-- Image element container -->
        <div class="container"></div>

        <!-- Script -->
        <script src='jquery-3.2.1.min.js' type='text/javascript'></script>
        <script type='text/javascript'>
            $(document).ready(function(){

                // Upload
                $("#but_upload").click(function(){

                    var fd = new FormData();
                    var files = $('#file')[0].files[0];
                    fd.append('file',files);
                    fd.append('request',1);

                    // AJAX request
                    $.ajax({
                        url: 'addremove.php',
                        type: 'post',
                        data: fd,
                        contentType: false,
                        processData: false,
                        success: function(response){
                            
                            if(response != 0){
                                var count = $('.container .content').length;
                                count = Number(count) + 1;

                                // Show image preview with Delete button
                                $('.container').append("<div class='content' id='content_"+count+"' ><img src='"+response+"' width='100' height='100'><span class='delete' id='delete_"+count+"'>Delete</span></div>");
                            }else{
                                alert('file not uploaded');
                            }
                        }
                    });
                });

                // Remove file
                $('.container').on('click','.content .delete',function(){
                    
                    var id = this.id;
                    var split_id = id.split('_');
                    var num = split_id[1];

                     // Get image source
                    var imgElement_src = $( '#content_'+num+' img' ).attr("src");
                     
                    var deleteFile = confirm("Do you really want to Delete?");
                    if (deleteFile == true) {
                        // AJAX request
                        $.ajax({
                           url: 'addremove.php',
                           type: 'post',
                           data: {path: imgElement_src,request: 2},
                           success: function(response){
                         
                                // Remove <div >
                                if(response == 1){
                                    $('#content_'+num).remove();
                                }

                           }
                        });
                    }
                });

            });
        </script>
    </body>
</html>

最后是config.php

<?php  
// Database configuration  
$dbHost     = "localhost";  
$dbUsername = "root";  
$dbPassword = "";  
$dbName     = "images";  
  
// Create database connection  
$db = new mysqli($dbHost, $dbUsername, $dbPassword, $dbName);  
  
// Check connection  
if ($db->connect_error) {  
    die("Connection failed: " . $db->connect_error);  
}
?>

我在想问题出在哪里,我代码直到我自己也搞不清楚。 有人能帮忙吗??


共1个答案

匿名用户

PHP实现存在一些问题。

具体地说,您使用的是

 $db->query("DELETE FROM images WHERE id = ? LIMIT 1");

看起来您的意图是使用准备好的语句,但目前您的查询什么也不做。 因为where id=?子句缺少正确的值

查看这篇关于使用mysqi_*和准备好的语句的文章。 https://SUPunkavinda.blog/php/prepared-statements