如果未插入图像,则不更新字段

时间:2018-07-29 18:23:30

标签: php mysql file-upload

我对PHP还是很陌生,但是我的思考过程围绕我正在编写的某些代码而出现问题。

我正在尝试使以下内容起作用,以便用户可以以表单的形式上载两个图像,该图像上载到服务器并更新SQL中的字段,但是我很难确定如何制作因此,除非上载图片,否则SQL字段不会更新-我设法使它使用一个图片;

def Postorder_iterative(head):
    if head is None:
        return None
    sta=stack()
    while True:
        while head is not None:
            if head.r:
                sta.push(head.r)
            sta.push(head)
            head=head.l
        if sta.top is -1:
            break
        head = sta.pop()
        if head.r is not None and sta.top is not -1  and head.r is sta.A[sta.top]:
            x=sta.pop()
            sta.push(head)
            head=x
        else:
            print(head.val,end = ' ')
            head=None
    print()    

我正在努力解决问题,如何为两张图片(最终超过两张图片?)

尝试了很多谷歌搜索,但还没有很多运气!

2 个答案:

答案 0 :(得分:0)

$uploadArtwork1 = $_FILES['asset_name1']['tmp_name'];
$uploadArtwork2 = $_FILES['asset_name2']['tmp_name'];

// Image1 and/or image2 was uploaded successfully
if(($uploadArtwork1 != null) || ($uploadArtwork2 != null)) {
    $sql = "";
// No images were selected, or there were problems uploading them
} else {
    $sql = ""; 
}

尽管最好检查$ _FILES ['asset_name'] ['error'] == UPLOAD_ERR_OK以确定图像是否成功上传:

$uploadArtwork1 = $_FILES['asset_name1']['error'];
$uploadArtwork2 = $_FILES['asset_name2']['error'];

// Image1 and/or image2 was uploaded successfully
if(($uploadArtwork1 == UPLOAD_ERR_OK) || ($uploadArtwork2 == UPLOAD_ERR_OK)) {
    // Do something with $_FILES['asset_name1']['tmp_name'] and $_FILES['asset_name2']['tmp_name']
    $sql = "";
// No images were selected, or there were problems uploading them
} else {
    $sql = ""; 
}

更新:

require_once("Inc/classCloud.php");

$sql = "UPDATE assets SET asset_title='$post_asset_title'";

if ($uploadArtwork != null) {

    $getImageID= $res['data'];
    $sql .= ", asset_name='$getImageID'";

}

if ($uploadMock != null) {

    $getImageID2= $res2['data'];
    $sql .= ", product_artwork='$getImageID2'";

}

$sql .= " WHERE asset_id='$post_asset_id'";

答案 1 :(得分:0)

这是可以使用的基本结构。

基本上循环浏览所有上载的文件,如果找到它们,则将它们移到服务器上的新位置,并将条目写入数据库。

此代码未经测试。

<?php

// Loops through all possible file uploads.
foreach ($_FILES as $file) {

    // Checks a file has been chosen.
    if (isset($file['tmp_name']) && !empty($file['tmp_name'])) {

        // Checks the uploaded (object) is a file.
        if (is_file($file['tmp_name'])) {

            // The filepath for the uploaded file.
            $destination = 'LOCATION TO MOVE THE UPLOADED FILE TO';

            /* 
             * Perform SQL Write here
             */

            if (WRITE WAS SUCCESSFUL) {

                // Move FIle
                move_uploaded_file($file['tmp_name'], $destination);    
            }
        }
    }
}