基于两个输入文件的数据库更新

时间:2014-10-31 17:24:17

标签: php html mysql

我正在使用具有多个输入文本和两个输入文件的表单来更新数据库表。

我设置了隐藏的两个输入,图像名称为值。

如果仅为imageFile设置了图片,则我需要更新该数据,imageFileBanner也是如此。

如果两者都设置了,我需要更新所有字段。

如何只更新一张图片?

    if(!empty($_FILES['imageFile']['name']) && empty($_POST['imageIsSet']) !empty($_FILES['imageFileBanner']['name'])){
        $sql = $database->update(sprintf("UPDATE taburi SET nume='%s', logo='%s', vizibil='%d', ordine='%d', banner='%s' WHERE id='%d'", $nume, $img, $status, $ordine, $img_banner, $id));

    }else{
        $sql = $database->update(sprintf("UPDATE taburi SET nume='%s', vizibil='%d', ordine='%d' WHERE id='%d'", $nume, $status, $ordine, $id));

    }  

如果我为ImageFile选择了一个文件,则会使用此语句,它会在更新时将我的imageFileBanner设置为空,反之亦然。

这是我的表格

      <form id="editTabForm" name="editTabForm" enctype="multipart/form-data">
        <div class="row">
          <div class="col-sm-12 col-md-12">
            <div class="info"><i class="fa fa-info"></i> Completati numele tab-ului apoi selectati o imagine pentru afisare.</div>
          </div>
        </div>
        <div class="row">
          <div class="col-sm-6 col-md-6">
            <div class="control-group form-group">
              <label for="tab-name">Nume tab</label>
              <input class="form-control" type="text" name="tab-name" id="tab-name" placeholder="nume tab" value="{$tab.nume}" />
            </div>
            <div class="control-group form-group">
              <label>Status</label>
              <select name="status" class="category-tree">
                {if $tab.vizibil == 1}
                <option value="1" selected="selected">Activ</option>
                <option value="0">Inactiv</option>
                {else}
                <option value="1">Activ</option>
                <option value="0" selected="selected">Inactiv</option>
                {/if}
              </select>
            </div>
            <div class="control-group form-group">
              <label for="tab-order">Ordinea afisarii in meniul orizontal</label>
              <input class="form-control" type="text" name="tab-order" id="tab-order" style="width:20%;" value="{$tab.ordine}"/>
            </div>
          </div>
          <div class="col-sm-6 col-md-6">
            <div class="control-group form-group">
              <label for="image">Selectati o imagine pentru afisarea tab-ului</label>
              <div id="imagePreview">
                {if $tab.logo}
                <img src="{$website}/media/taburi/{$tab.logo}" class="imagePreview thumbnail" style="max-width:50px; max-height:50px;" />
                {else}
                <img src="/admin/mmadmin/template/assets/img-upload-50_50.png" class="imagePreview thumbnail" style="max-width:50px; max-height:50px;" />
                {/if}
              </div>
              <input type="file" name="imageFile" id="image" />
            </div>
            <div class="control-group form-group">
              <label for="image">Selectati o imagine default pentru tab</label>
              <div id="imagePreviewBanner">
                {if $tab.banner}
                <img src="{$website}/media/cat/{$tab.banner}" class="imagePreviewBanner thumbnail" style="max-width:150px; max-height:150px;" />
                {else}
                <img src="/admin/mmadmin/template/assets/img-upload-150_150.png" class="imagePreviewBanner thumbnail" style="max-width:150px; max-height:150px;" />
                {/if}
              </div>
              <input type="file" name="imageFileBanner" id="imageBanner" />
              <input type="hidden" name="id" value="{$tab.id}" />
              <input type="hidden" name="imageIsSet" value="{$tab.logo}" />
              <input type="hidden" name="bannerIsSet" value="{$tab.banner}" />
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Anuleaza</button>
          <button type="button" class="btn btn-primary" id="update-tab" data-id="{$tab.id}">Salveaza</button>
        </div>
      </form>

enter image description here

1 个答案:

答案 0 :(得分:0)

解决::
还有更好的办法吗?

if(!empty($_FILES['imageFile']['name'])){

            $sql = $database->update(sprintf("UPDATE taburi SET nume='%s', logo='%s', vizibil='%d', ordine='%d' WHERE id='%d'", $nume, $img, $status, $ordine, $id));

        }else if(!empty($_FILES['imageFileBanner']['name'])){

            $sql = $database->update(sprintf("UPDATE taburi SET nume='%s', vizibil='%d', ordine='%d', banner='%s' WHERE id='%d'", $nume, $status, $ordine, $img_banner, $id));

        }else if(!empty($_FILES['imageFile']['name']) && !empty($_FILES['imageFileBanner']['name'])){

            $sql = $database->update(sprintf("UPDATE taburi SET nume='%s', logo='%s', vizibil='%d', banner='%s', ordine='%d' WHERE id='%d'", $nume, $img, $status, $ordine, $img_banner, $id));

        }else{

            $sql = $database->update(sprintf("UPDATE taburi SET nume='%s', vizibil='%d', ordine='%d' WHERE id='%d'", $nume, $status, $ordine, $id));

        }