2013-03-18 20 views
-1

我正在使用以下表單來創建相冊,它將數據提交給處理腳本,然後處理這些文件並將數據輸入數據庫。正在使用一個插入創建Mysqli 2記錄

這是提交表單:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Create New Album</title> 
    </head> 

    <body> 
    <p>Create New Album</p> 
    <form action="createnewalbumsubmit.php" method="post" enctype="multipart/form-data" name="form1" id="form1"> 

     <input type="hidden" value="<?php echo substr(md5(time() * rand()),0,10); ?>" name="albumid" id="albumid" /> 
     <input type="hidden" value="<?php echo date("Y-m-d"); ?>" name="datecreated" id="datecreated" /> 
     <input type="hidden" value="yes" name="isalbum" id="isalbum" /> 

     <p> 
     <label for="albumname">Album Name</label> 
     <input type="text" name="albumname" id="albumname" /> 
     </p> 
     <p> 
     <label for="albumthumbnail">Album Thumbnail Image</label> 
     <input type="file" name="albumthumbnail" id="albumthumbnail" /> 
     </p> 
     <p> 
     <input type="submit" name="submit" id="submit" value="Submit" /> 
     </p> 
    </form> 
    </body> 
    </html> 

這是數據處理用腳本,它使用的是VEROT上傳類與上傳的文件處理和THEN的mysqli添加的詳細信息與數據庫:

<?php include("connect.php"); ?> 
    <?php 
    // Posted Data 
    if(isset($_POST['albumid'])){ 
     $albumid = $_POST['albumid'];}; 

     if(isset($_POST['datecreated'])){ 
     $datecreated = $_POST['datecreated'];}; 

     if(isset($_POST['isalbum'])){ 
     $isalbum = $_POST['isalbum'];}; 

     if(isset($_POST['albumname'])){ 
     $albumname = $_POST['albumname'];}; 
     // 


     require_once 'uploadclass/class.upload.php'; 

     $file = new Upload($_FILES['albumthumbnail']); 
    if ($file->uploaded) { 
     // save uploaded image with a new name, 
     // resized to 100px wide 
     $albumthumbnail = substr(md5(time() * rand()),0,10); 
     $file->file_new_name_body = $albumthumbnail; 
     $file->image_resize = true; 
     $file->image_convert = 'jpg'; 
     $file->image_x = 100; 
     $file->image_ratio_y = true; 
     $file->Process('albums/'.$albumid.'/thumbnail/'); 
     $filename = $file->file_dst_name; 
     if ($file->processed) { 
     echo 'image renamed, resized x=100 
       and converted to jpg'; 
     $file->Clean(); 
     } else { 
     echo 'error : ' . $file->error; 
     } 
    } 

    mysqli_query($db,"INSERT INTO albums (`albumid`,`datecreated`,`isalbum`,`albumname`,`albumthumbnail`) VALUES ('$albumid','$datecreated','$isalbum','$albumname','$filename')"); 

    ?> 

我遇到的問題是,當我創建一個新記錄時,兩個記錄正在數據庫中創建,一個空白記錄中沒有任何內容,另一個記錄中包含所添加相冊的所有詳細信息。

enter image description here

回答

4

這是因爲你,如果窗體被髮布正在不檢查。每次登陸頁面上時,它將運行:

mysqli_query($db, "INSERT INTO albums (`albumid`,`datecreated`,`isalbum`,`albumname`,`albumthumbnail`) VALUES ('$albumid','$datecreated','$isalbum','$albumname','$filename')"); 

這就是爲什麼你得到一個空白記錄。您需要圍繞您的提交代碼if (!empty($_POST)) { }這樣:

<?php 
include ("connect.php"); 

if (!empty($_POST)) { 
    // Posted Data 
    if (isset($_POST['albumid'])) { 
     $albumid = $_POST['albumid']; 
    }; 

    if (isset($_POST['datecreated'])) { 
     $datecreated = $_POST['datecreated']; 
    }; 

    if (isset($_POST['isalbum'])) { 
     $isalbum = $_POST['isalbum']; 
    }; 

    if (isset($_POST['albumname'])) { 
     $albumname = $_POST['albumname']; 
    }; 
    // 

    require_once 'uploadclass/class.upload.php'; 

    $file = new Upload($_FILES['albumthumbnail']); 
    if ($file -> uploaded) { 
     // save uploaded image with a new name, 
     // resized to 100px wide 
     $albumthumbnail = substr(md5(time() * rand()), 0, 10); 
     $file -> file_new_name_body = $albumthumbnail; 
     $file -> image_resize = true; 
     $file -> image_convert = 'jpg'; 
     $file -> image_x = 100; 
     $file -> image_ratio_y = true; 
     $file -> Process('albums/' . $albumid . '/thumbnail/'); 
     $filename = $file -> file_dst_name; 
     if ($file -> processed) { 
      echo 'image renamed, resized x=100 
       and converted to jpg'; 
      $file -> Clean(); 
     } else { 
      echo 'error : ' . $file -> error; 
     } 
    } 

    mysqli_query($db, "INSERT INTO albums (`albumid`,`datecreated`,`isalbum`,`albumname`,`albumthumbnail`) VALUES ('$albumid','$datecreated','$isalbum','$albumname','$filename')"); 
} 
?> 
+0

感謝您的幫助,這是現在整理它! – 2013-03-18 15:32:58

+0

請注意,推薦的方式來檢查它是否是一個POST請求,通過檢查它是這樣的:if($ _ SERVER ['REQUEST_METHOD'] =='POST')... – 2013-03-18 15:48:29

相關問題