php - 如何在php文件上传中从mysql读取当前记录的id?

标签 php mysql

如何从当前数据库 sobarticles 的当前记录中读取以下代码,数据字段 id 和将其写入 $current_id

一般来说,我想更改上传文件的文件名,总是先更改“ID2”(此 ID2 对于当前操作不会更改),然后更改文件名。

下面的代码只给我 $new_id 的 000000

我已经被封锁一周了,不知道。我尝试了很多事情。我读了这么多。我学到了很多东西,但我没有得到结果。真是令人沮丧……

这是“body_articles.php”中的html代码

            <?php session_start();

                $_db_host = "localhost";
                $_db_username = "admin0";
                $_db_passwort = "dfgfghxyxc";
                $_db_datenbank = "sob";

                # Verbindung zur Datenbank herstellen
                $_link = mysql_connect($_db_host, $_db_username, $_db_passwort);

                # Pr�fen ob die Verbindung geklappt hat
                if (!$_link)
                {
                    # Nein, also das ganze Skript abbrechen !
                    die("Keine Verbindung zur Datenbank m�glich: " .
                        mysql_error());
                }

                # Datenbank ausw�hlen
                mysql_select_db($_db_datenbank, $_link);

            ?>

            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">

            <head>
                <meta charset="UTF-8" />

                <title>SoB - Administration</title>

                <link rel="stylesheet" type="text/css" href="css/style.css">


                <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>

                <script type="text/javascript" src="js/multiupload.js"></script>

                <script type="text/javascript">

                    var config =
                    {
                        support : "image/jpg,image/png,image/bmp,image/jpeg,image/gif",     // Valid file formats
                        form: "demoFiler",                  // Form ID
                        dragArea: "dragAndDropFiles",       // Upload Area ID
                        uploadUrl: "upload.php"             // Server side upload url
                    }

                    $(document).ready(function(){
                        initMultiUploader(config);
                    });


                    $(document).ready(function() {
                        var storedFiles = [];
                        $('#myfiles').on('change', function() {
                            $('#messages').html('');
                            var myfiles = document.getElementById('myfiles');
                            var files = myfiles.files;
                            var i=0;
                            alert("files uploading");
                            for (i = 0; i<files.length; i++) {
                                var readImg = new FileReader();
                                var file=files[i];
                                if(file.type.match('image.*')){
                                    storedFiles.push(file);
                                    readImg.onload = (function(file) {
                                        return function(e) {
                                            $('#uploadedfiles').append('<tr class="imageinfo"><td><img width="80" height="70" src="'+e.target.result+'"/></td><td>'+file.name+'</td><td>'+Math.round((file.size/1024))+'KB</td><td><a href="" class="lnkcancelimage" file="'+file.name+'" title="Cancel"><img src="delete.png" width=34" height="34"/></a></td></tr>');
                                        };
                                    })(file);
                                    readImg.readAsDataURL(file);
                                }else{
                                    alert('the file '+file.name+' is not an image<br/>');
                                }
                            }
                        });

                        $('#uploadedfiles').on('click','a.lnkcancelimage',function(){
                            $(this).parent().parent().html('');
                            var file=$(this).attr('file');
                            var strID = myTrim(document.demoFiler.ID2.value);
                            for(var i=0;i<storedFiles.length;i++) {
                                if(storedFiles[i].name == file) {
                                    storedFiles.splice(i,1);
                                    break;
                                }
                            }
                            return false;
                        });

                        $('#lnkupload').click(function(){
                            var data = new FormData();
                            var i=0;
                            for(i=0; i<storedFiles.length; i++) {
                                data.append('files'+i,storedFiles[i]);
                            }

                            if(i>0){
                                $.ajax({
                                    url: 'load.php',
                                    type: 'POST',
                                    contentType: false,
                                    data: data,
                                    processData: false,
                                    cache: false
                                }).done(function(msg) {
                                        storedFiles = [];
                                        if(msg){
                                            alert(msg);
                                        }else{
                                            $('#messages').html('Images uploaded successfully');
                                        }
                                    }).fail(function() {
                                        alert('error');
                                    });
                            }
                            return false;
                        });

                    });

                  function jsShowArticle() {
                      js_articles = JSON.parse(json_string);
                      $('[name="recordCurrent"]').attr("id",js_articles[0]);
                      document.demoFiler.ID2.value = js_articles[0];
                  }

                </SCRIPT>

                <style type="text/css">.buttonarea: (\a)</style>

            </head>

            <body class="page page-id-11505 page-template-default" onload="jsShowArticle();">

            <div id="page-wrap">

            <?php
            include('includes/header.html');
            ?>

            <div id="container-main">
                <div id="main-content">

                    <div class="post" id="post-11505">

                        <title>SoB - Administration</title>

                        <div class="entry">

                            <div id="dragAndDropFiles" class="uploadArea">
                            <br>
                            <span style="padding-left: 20px">To upload more pictures for this item click Browse</span>
                            <br>
                            <span style="padding-left: 20px">The order of the upload decide the order to show the pictures</span>

                            </div>

                            <form name="demoFiler" id="demoFiler" enctype="multipart/form-data" style="">
                                <input id="ID2" type="text" name="ID2name">
                                <input type="file" name="multiUpload" id="multiUpload" multiple />
                                <input type="submit" name="submitHandler" id="submitHandler" value="Upload" class="buttonUpload" />

                            </form>

                            <div class="progressBar">
                                <div class="status"></div>
                            </div>

                        </div>

                    </div>

                </div>

                <div id="aside">

                </div>

                <br class="clearfloat" />
            </div> <!-- End of main container -->

            </div><!-- END Page Wrap -->

            <div id="footer">
                <br class="clearfloat" />

                <?php
                if(isset ($_SESSION['name']))
                {
                    $loginTitle="Logout";
                    $loginLink="body_logout.php";
                }
                else
                {
                    $loginTitle="Login";
                    $loginLink="body_login.php";
                }
                ?>

            </div>

            </body>

            </html>

文件上传.php

            <?php
            $link = mysql_connect('localhost', 'admin0', 'fdhfuirkfk');
            if (!$link) {
                die('Could not connect: ' . mysql_error());
            }

            $db_selected = mysql_select_db('sob', $link);
            if (!$db_selected) {
                die('Can\'t use foo : ' . mysql_error());
            }

            $current_id = (int)$_REQUEST['currentRecord']; // here is Your current_id

            // if You're just getting data so You echo result from db
            if($_SERVER['REQUEST_METHOD'] == "GET"){
                $result = mysql_query("SELECT * FROM articles WHERE id > " . $current_id . " ORDER BY id ASC LIMIT 1");
                $result = mysql_fetch_row($result);
                echo json_encode($result);
                exit(0);
            }

            // and there You're pushing file and echo $_POST['index']
            if($_SERVER['REQUEST_METHOD'] == "POST"){
                $new_id = sprintf( "%06d", $current_id);
                if(move_uploaded_file($_FILES['file']['tmp_name'], "sobimages/" . $new_id . $_FILES['file']['name'])){
                    echo($_POST['index']);
                }
                exit(0);
            }?>

如何从表单字段ID2获取值并将该值读入$new_idupload.php文件中而不干扰其他行动?

      • +

最后一个文件是 nextarticle.php 作为示例。它与多文件上传无关,我发帖只是为了提供更多信息。它读取下一条记录。

<?php
    $link = mysql_connect('localhost', 'admin0', 'fdhfuirkfk');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }
    $db_selected = mysql_select_db('sob', $link);
    if (!$db_selected) {
        die('Can\'t use foo : ' . mysql_error());
    }

    $current_id = $_REQUEST['currentRecord'];
    $result = mysql_query("SELECT * FROM articles WHERE id > " . $current_id . " ORDER BY id ASC LIMIT 1");
    $result = mysql_fetch_row($result);
    echo json_encode($result);
?>

最佳答案

试试这个:

$link = mysql_connect('parsley.arvixe.com:3306', 'admin0', 'fdhfuirkfk');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}

$db_selected = mysql_select_db('sob', $link);
if (!$db_selected) {
    die('Can\'t use foo : ' . mysql_error());
}

$current_id = (int)$_REQUEST['currentRecord']; // here is Your current_id

// if You're just getting data so You echo result from db
if($_SERVER['REQUEST_METHOD'] == "GET"){
    $result = mysql_query("SELECT * FROM articles WHERE id > " . $current_id . " ORDER BY id ASC LIMIT 1");
    $result = mysql_fetch_row($result);
    echo json_encode($result);
    exit(0);
}

// and there You're pushing file and echo $_POST['index']
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $new_id = sprintf( "%06d", $current_id);
    if(move_uploaded_file($_FILES['file']['tmp_name'], "sobimages/" . $new_id . $_FILES['file']['name'])){
        echo($_POST['index']);
    }
    exit(0);
}

关于php - 如何在php文件上传中从mysql读取当前记录的id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30652552/

相关文章:

php - 如何使用自定义错误处理程序捕获 php E_PARSE 错误?

mysql - Rails 3.1 破坏了 MySql::Time 的排序

php - mysql联结可以吗?

mysql - 错误 1415 : Not allowed to return a result set from a function

MySQL 错误 1062 : Duplicate entry for key

javascript - 输入数据超出限制如何提示?

php - PHP $_POST 数组中的未定义索引

javascript - 创建动态更新表单,源自 MYSQL DB

php - 有没有更好的方法来设置来自 php 脚本中具有相似值的表单的两个输入字段的四个不同变量

php - Laravel 无法在构造函数中设置类变量