检查用户是否在表单中添加新图像

时间:2016-09-17 12:35:58

标签: php

我需要创建其他动作。如果用户不上传新图像,则在数据库中应保留旧图像。添加图像的代码是:

var url: URL!

// You can check if the variable is initialized by checking it against nil:
//     if url == nil { /* not initialized */ }

// When you are ready to assign it a value:
url = URL(string: "https://www.apple.com")

我不知道如何创建其他动作

1 个答案:

答案 0 :(得分:0)

您可以尝试这些行 - 默认情况下变量$content_pic设置为false,因此如果没有上传图像,则可以执行不同的sql语句。

<?php

    if( isset( $_POST['id'], $_POST['title'], $_POST['text'] ) ){

        require_once 'database.php';
        require_once 'functions.php';


        $id     =   $_POST['id'];
        $title  =   trim( $_POST['title'] );
        $text   =   trim( $_POST['text'] );
        $field  =   'content_pic';


        /* will be populated if new upload detected */
        $content_pic = false;



        if( !empty( $_FILES[ $field ] ) ) {

            $file=$_FILES[ $field ];

            if( $file["error"] == UPLOAD_ERR_OK ) {

                $uploads_dir    = __DIR__ . '/files';
                $tmp_name       = $file['tmp_name'];
                $name           = $file['name'];
                $savepath       = $uploads_dir . DIRECTORY_SEPARATOR . $name;



                if( move_uploaded_file( $tmp_name, $savepath ) ) {
                    $content_pic = realpath( $savepath ) ? "files/{$name}" : false;
                }
            }
        }


        switch( $content_pic ){
            case false:
                $sql='update `content` set `title`=? `text`=? where `id`=?';
                $params=array( $title, $text, $id );
            break;
            default:
                $sql='update `content` set `title`=? `text`=?, `content_pic`=? where `id`=?';
                $params=array( $title, $text, $content_pic, $id );
            break;
        }

        $db = database_connect();
        $stmt = $db->prepare( $sql );
        $stmt->execute( $params );
        $db->close();
    }

?>