2012-07-31 75 views
0

MySQL表看起來是這樣的: enter image description here高級PHP上傳表單

下面是輸出的代碼:

<li> 
    <a class="thumb" href="library/<?php echo $row2['projectname']; ?>/<?php echo $row2['projectrecordnum']; ?>.<?php echo $row2['projectformat']; ?>" title="<?php echo $row2['projectrecordtitle']; ?>"> 
     <img src="library/<?php echo $row2['projectname']; ?>/thumbs/<?php echo $row2['projectrecordnum']; ?>.<?php echo $row2['projectformat']; ?>" alt="<?php echo $row2['projectrecordtitle']; ?>" /> 
    </a> 
    <div class="caption"> 
     <div class="image-title"><?php echo $row2['projectrecordtitle']; ?></div> 
     <div class="image-desc"><?php echo $row2['projectrecorddesc']; ?></div> 
     <div class="download"> 
      <a href="library/<?php echo $row2['projectname']; ?>/<?php echo $row2['projectrecordnum']; ?>.<?php echo $row2['projectformat']; ?>" target="_blank">Download Original</a> 
     </div> 
    </div> 
</li> 

下面是輸出的外觀:

<li> 

    <a class="thumb" href="library/ultima/2.jpg" title="Timbo DISPLACED"> 

     <img src="library/ultima/thumbs/2.jpg" alt="Timbo DISPLACED" /> 

    </a> 

    <div class="caption"> 

     <div class="image-title">Timbo DISPLACED</div> 

     <div class="image-desc">Made in 3ds Max, background, color and lens flare from Photoshop</div> 

     <div class="download"> 

      <a href="library/ultima/2.jpg" target="_blank">Download Original</a> 

     </div> 

    </div> 

</li> 

我的問題是,我無法找到一種形式,這將創建一個用戶需要的領域,然後將每個文件信息上傳並寫入一個mysql數據庫(請記住文件數量不確定)。

+0

到底是什麼你的問題?你的'表格'看起來像什麼? – Peon 2012-07-31 14:14:30

+0

目前它只是載有項目名稱,項目顯示名稱,項目格式,項目評論,項目信息 – 2012-07-31 14:26:01

+0

等主要信息所以你想要一個系統,將創建一個未知數量/任意內容的表單字段設置,並處理填充那些誰知道,什麼字段到數據庫中? – 2012-07-31 14:28:29

回答

1

這不是一個真正具體的問題,但我會盡力爲您提供一個起點。

我假設你沒有一個形式尚未...

FYI:不介意與htmlentities()或類似的東西來逃避輸出!

1)我用假數據,但你可以與你的關聯DB結果集替換此:

$rows = array(
    array( 'projectname' => 'ultima', 
      'projectrecordnum' => '1', 
      'projectrecordtitle' => 'The ball of destruction'), 
    array( 'projectname' => 'ultima', 
      'projectrecordnum' => '2', 
      'projectrecordtitle' => 'Timbo DISPLACED'), 
    array( 'projectname' => 'ultima', 
      'projectrecordnum' => '3', 
      'projectrecordtitle' => 'Exposure') 
); 

2)我創建了一個簡單的HTML表單通過PHP成果循環:

<form method="post" action="#"> 
<?php foreach($rows as $row) { ?> 
    <div class="caption"> 
     <div class=""> 
      <input type="text" name="projectname[]" value="<?php echo $row['projectname']; ?>"> 
     </div> 
     <div class=""> 
      <input type="text" name="projectrecordnum[]" value="<?php echo $row['projectrecordnum']; ?>"> 
     </div> 
     <div class=""> 
      <input type="text" name="projectrecordtitle[]" value="<?php echo $row['projectrecordtitle']; ?>"> 
     </div> 
     <?php /* and so more fields ... */ ?> 
    </div> 
    <hr /> 
<?php } ?> 
    <input type="submit" name="send" value="send" /> 
</form> 

3)和action你可以重建你$rows陣列:

if(isset($_REQUEST['send'])) { 
    $rows = array(); 
    for($i = 0; $i < count($_POST['projectname']); $i++) { 
     $row = array(
      'projectname' => $_POST['projectname'][$i], 
      'projectrecordnum' => $_POST['projectrecordnum'][$i], 
      'projectrecordtitle' => $_POST['projectrecordtitle'][$i], 
     ); 
     if(! empty($_POST['projectname'][$i])) { 
      array_push($rows, $row); 
     } 
     unset($row); 
    } 

    // Your data array... 
    print '<pre>'. print_r($rows, true) . '</pre>'; 
} 
+0

感謝您的回覆!這非常有用。但是你誤解了我一點。字段數應由用戶定義,而不是由可用行定義。和 – 2012-07-31 16:04:20

+0

嗯...如果用戶輸入的字段與數據庫表中的列不匹配,會發生什麼情況? – 2012-07-31 16:08:09

+0

我找到了解決方案 - while循環! – 2012-08-01 11:24:55