dqt83336 2018-11-23 02:52
浏览 35

使用POST提交到数据库不会显示新的更改

I am submitting an image to the db like this:

if (isset($_POST['uploadImgCustom'])) {
    $myNewImg = get_post_meta($id, 'usp-file-single', true);
}
$attachments = get_posts(array( 
    'post_type' => 'attachment',
    'numberposts' => -1,
    'post_status' =>'any',
    'post_parent' => $post->ID
));

if ($attachments) {
    foreach ( $attachments as $attachment ) {
        $myNewImg = wp_get_attachment_url( $attachment->ID );
        update_post_meta( $id, 'usp-file-single', $myNewImg);
    } 
?>
    <img src="<?php echo $myNewImg; ?>" class="img-responsive">
<?php }

And then I do:

if($_SERVER['REQUEST_METHOD']=="POST") {
    if ('AGGIORNA ALLEGATO' === ($_POST['uploadImgCustom'])) {
        if ($_FILES['postImage']) {
            $attachments = get_attached_media( '', $id );
            foreach ($attachments as $attachment) {
                wp_delete_attachment( $attachment->ID, 'true' );
            }   
            foreach ($_FILES as $file => $array) {
                if ($_FILES[$file]['error'] !== UPLOAD_ERR_OK) {
               //Add your error action
                } else {    
                    $attach_id = media_handle_upload( $file, $id );
                }
            }   
        }
    }
}

The new file is uploaded correctly and the page refreshes itself after upload completed. Yet I am seeing the old image on the page, however if I refresh the page, I see the new file. It is like keeping the cache and not reading the new data at first.

The following it is the current post ID which I grab at the beginning of the code right after I start the loop.

$id = get_the_ID();

The form html is the following:

<div class="row">
    <div class="col-xs-12">
        <h3>CARICA UNA NUOVA IMMAGINE O VIDEO</h3>
        <form method="POST" action="" enctype="multipart/form-data">
            <div class="form-group">
                <input type="file" name="postImage" multiple="multiple" class="form-control">
            </div>
            <input id="uploadImg" name="uploadImgCustom" type="submit" value="AGGIORNA ALLEGATO" class="btn secondary-btn primary-bg">
        </form>
    </div>
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 MATLAB中streamslice问题
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端