2017-07-24 183 views
0

所以......這個問題正在導致我在這個時候失去頭髮。MySQL PHP沒有插入所有的數據到數據庫

我在我的網站上有一個表格,允許用戶將圖片上傳到圖庫文件夾,並且它工作正常。它上傳文件,將其插入到數據庫中,然後我可以進入畫廊並顯示它。

問題是,出於某種原因,逃避我,它不會插入$ _POST ['標題']變量到數據庫中。當你點擊提交時它甚至不會捕獲它。所以現在我有幾張圖片沒有標題列出,即使有一個被輸入框中。 (請注意,我有一個檢查確保該字段不爲空,並且在運行檢查時不會出現錯誤)。

這裏是我的PHP和形式的部分代碼:

if(isset($_POST['submit'])) 
{ 
    $caption = trim($_POST['caption']); 
    $category = trim($_POST['gallery']); 

    if($caption = '') 
    { 
     $error .= '<p class="error">Please enter a caption for your image.</p>'; 
    } 

    if($gallery = '') 
    { 
     $error .= '<p class="error">Please select a gallery for your image.</p>'; 
    } 

    //Begin upload checks 
    $dir = "../gallery/"; 
    $maxsize = 5000000; // 5MB 
    $valid_exts = array('jpeg','jpg','png','gif'); 
    $ok = 1; 

    if(isset($_FILES['file'])) 
    { 
     $target_file = $dir . basename($_FILES['file']['name']); 
     if($_FILES['file']['size'] < $maxsize) 
     { 
      // get file extension 
      $ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION)); 
      if(in_array($ext, $valid_exts)) 
      { 
       if(file_exists($target_file)) 
       { 
        $error .= '<p class="error">File already exists.</p>'; 
        $ok = 0; 
       } 
       else 
       { 
        $ok = 1; 
       } 
      } 
      else 
      { 
       $error .= '<p class="error">Image must be a png, gif, or jpg/jpeg file.</p>'; 
       $ok = 0; 
      } 
     } 
     else 
     { 
      $error .= '<p class="error">Image must be no larger than 5MB.</p>'; 
      $ok = 0; 
     } 
    } 
    else 
    { 
     $error .= '<p class="error">No image was selected for upload.</p>'; 
     $ok = 0; 
    } 

    if(empty($error) && $ok == 1) 
    { 
     if(move_uploaded_file($_FILES['file']['tmp_name'], $target_file)) 
     { 
      $date = date('m-d-Y'); 
      $stmt = $db->prepare('INSERT INTO gallery_photos (photo_filename,photo_caption,photo_category,postdate) VALUES (?,?,?,STR_TO_DATE(?, "%m-%d-%Y"))'); 
      if($stmt) 
      { 
       $stmt->bind_param('ssss',$_FILES['file']['name'],$caption,$category,$date); 
       if($stmt->execute()) 
       { 
        $success .= '<p class="success">File successfully uploaded to the gallery.</p>'; 
       } 
       else 
       { 
        $error .= '<p class="error">Error code 89. Please contact the site administrator.</p>'; 
       } 
      } 
      else 
      { 
       $error .= '<p class="error">Error code 86. Please contact the site administrator.</p>'; 
      } 
     } 
     else 
     { 
      $error .= '<p class="error">An error occured while uploading your file.</p>'; 
     } 
    } 
} 

?> 

<div id="form"> 
    <form action="" method="post" enctype="multipart/form-data" name="upload_form"> 
    <table cellspacing="2" cellpadding="2" width="500"> 
     <tr><th colspan="2">Upload Image</th></tr> 

     <tr><td colspan="2"> 
     <?php 
     if($error) 
     { 
      echo $error; 
     } 

     if($success) 
     { 
      echo $success; 
     } 

     if($caption) 
     { 
      echo $caption; 
     } 
     ?> 

     <p>Only PNG files are allowed.</p> 
     </td></tr> 

     <tr> 
      <td align="right"><label for="gallery">Gallery</label></td> 
      <td> 
       <select name="gallery"> 
        <option value="">Select One...</option> 
        <?php 
        $result = $db->query('SELECT * FROM gallery_category'); 
        if(is_object($result) && $result->num_rows > 0) 
        { 
         while($row = $result->fetch_array()) 
         { 
          echo '<option value="'.$row['category_id'].'">'.$row['category_name'].'</option>'; 
         } 
        } 
        ?> 
       </select> 
      </td> 
     </tr> 

     <tr> 
      <td align="right"><label for="file">Image</label></td> 
      <td><input type="file" name="file" /></td> 
     </tr> 

     <tr> 
      <td align="right"><label for="caption">Caption</label></td> 
      <td><input type="text" name="caption" /></td> 
     </tr> 

     <tr><td align="center" colspan="2"><input type="submit" name="submit" value="Upload Image"</td></tr> 
    </table> 
    </form> 
</div> 

在確定這個問題的任何幫助,將不勝感激,因爲我似乎無法找到它。在我的日誌或有問題的頁面中不會發生任何錯誤,並且將其插入到數據庫中而沒有錯誤,並且圖像上傳時沒有問題。

回答

1

問題來自您的支票if($caption = '')if($gallery = '')。因爲=是一個賦值操作符,所以不做比較。它會將您的$caption指定爲'',預計空白字幕的結果。你應該改變if($caption == '')if($gallery == '')

+1

...和'$ _ POST [「畫廊」]'實際上是設置爲'$ category'而不是'$畫廊「,這就是爲什麼這仍然進一步下降。 –

+0

感謝您的快速回復。我覺得這種疏忽很愚蠢。我調整了一些字段名稱,並且必須忘記在PHP檢查中更新它們。 – Bryan

+0

@Bryan:這個錯誤對於開發者和我來說也是很常見的,不要覺得自己很愚蠢。實際上,我發現人們使用快速技巧來防止在大多數語言中出現這種情況:總是將常量放在比較語句的左側: 'if('a'= $ c)'將引發錯誤而不是靜默分配$ c到'a'。 – minhhn2910

0

1)您指定因爲單= $的標題和$畫廊,而不是檢查

if($caption = ''){ } 

將$標題設置爲「」,也​​不會檢查。因此,標題是空

你應該檢查這種方式

if($caption == ''){ } 

與==

也許你也應該嘗試

if($caption == NULL){ } 

if(empty($caption)){ } 

2) $category = trim($_POST['gallery']);

我不知道你要這樣說,也許你應該看看