繁体   English   中英

如何读取mysql当前上传的php文件中记录的ID?

[英]How to read in php file upload from mysql current record the id?

我怎样才能在下面的代码从当前记录从当前数据库中读取sob从表中articles数据字段id ,并将其写入$current_id

一般而言,我要更改上载文件的文件名,总是先更改“ ID2”(此ID2对于当前操作不会更改),然后更改文件名。

下面的代码仅给我$ 000000作为$ new_id

我已经有一个星期被封锁了,不知道。 我尝试了很多事情。 我读了很多。 我学到了很多东西,但没有得到结果。 令人沮丧...

这是“ 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>

文件upload.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);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM