2014-02-26 51 views
1

我想製作一個表單,用戶可以提交5張圖片。提交的圖像名稱會保存到我的MSSQL表格列照片,照片1,照片2,照片3和照片4中。這些列全部在同一個表new_trailers1中。如果用戶僅在前三個輸入中上傳圖像並留下最後兩個輸入,則應將列設置爲photo = image.jpg photo1 = image2.jpg photo2 = image3.jpg photo3 = NULL和photo4 = NULL。目前,圖像被保存爲空而不是NULL。例如,photo3的列設置爲。這裏是我用我的PHP代碼當用戶提交表格:如果沒有文件被提交將SQL列設置爲NULL

if(isset($_POST['add'])) { 
    // Make sure the file was sent without errors 
    if($_FILES['uploaded_file']['error'] == 0) { 

    // Connect to the database 
    $conn = mssql_connect('gace.com','user','pass'); 
    mssql_select_db('ger',$conn); 

     // Gather all required data 

     //IMAGES 

     //This is the directory where images will be saved 
     $target = "unitimages/"; 
     $target = $target . basename($_FILES['photo']['name']); 
     $target1 = "unitimages/"; 
     $target1 = $target1 . basename($_FILES['photo1']['name']); 
     $target2 = "unitimages/"; 
     $target2 = $target2 . basename($_FILES['photo2']['name']); 
     $target3 = "unitimages/"; 
     $target3 = $target3 . basename($_FILES['photo3']['name']); 
     $target4 = "unitimages/"; 
     $target4 = $target4 . basename($_FILES['photo4']['name']); 
     // Where PDF is saved 
     $target5 = "unitimages/"; 
     $target5 = $target5 . basename($_FILES['uploaded_file']['name']); 

      $pic1=($_FILES['photo']['name']); 
      $pic2=($_FILES['photo1']['name']); 
      $pic3=($_FILES['photo2']['name']); 
      $pic4=($_FILES['photo3']['name']); 
       $pic5=($_FILES['photo4']['name']); 
       $name=($_FILES['uploaded_file']['name']); 

     if(! get_magic_quotes_gpc()) 
     { 
      $title = addslashes ($_POST['title']); 
      $desc = addslashes ($_POST['description']); 
      $make = addslashes ($_POST['make']); 
      $model = addslashes ($_POST['model']); 
      $year = addslashes ($_POST['year']); 
      $price = addslashes ($_POST['price']); 
     } 
     else 
     { 
      $title = $_POST['title']; 
      $desc = $_POST['description']; 
      $make = $_POST['make']; 
      $model = $_POST['model']; 
      $year = $_POST['year']; 
      $price = $_POST['price']; 
} 

    $columnid_query=mssql_query("SELECT MAX(orderid) FROM new_trailers1", $conn); 
    $columnid_array=mssql_fetch_row($columnid_query); 
    $newcolumnid=$columnid_array[0]+1; 

     // Create the SQL query 
$query = ("INSERT INTO new_trailers1 (title, orderid, description, make, model, year, price, name, photo, photo1, photo2, photo3, photo4) VALUES ('$title', '$newcolumnid', '$desc', '$make', '$model', '$year', '$price', '$name', '$pic1', '$pic2', '$pic3', '$pic4', '$pic5')") ; 

     // Execute the query 
    $result = mssql_query($query, $conn) or die(mssql_get_last_message()); 

     // Check if it was successfull 
     if($result) { 
      $updater = "UPDATE new_trailers1 SET (photo=NULL, photo1=NULL, photo2=NULL, photo3=NULL, photo4=NULL) WHERE (photo='',photo1='',photo2='',photo3='',photo4='')"; 
      $updateda = mssql_query($updater, $conn) or die(mssql_get_last_message()); 
      if($updateda) { echo'<script> 
alert("Suasd added successfully!"); 
</script>'; } 
      echo '<script> 
alert("Success! New Unit was added successfully!"); 
</script>'; 
     echo '<script>window.location.replace("newunitsmanagement.php");</script>'; 
     } 
     else { 
      echo 'Error! Failed to insert the file' 
       . "<pre>{$conn->error}</pre>"; 
     } 

//Writes the photos to the server 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

//Tells you if its all ok 
//echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
//echo "Sorry, there was a problem uploading your image."; 
} 
    if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target1)) 
{ 

//Tells you if its all ok 
// echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
//echo "Sorry, there was a problem uploading your image."; 
} 
    if(move_uploaded_file($_FILES['photo2']['tmp_name'], $target2)) 
{ 

//Tells you if its all ok 
//echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
//echo "Sorry, there was a problem uploading your image."; 
} 
    if(move_uploaded_file($_FILES['photo3']['tmp_name'], $target3)) 
{ 

//Tells you if its all ok 
//echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
//echo "Sorry, there was a problem uploading your image."; 
} 
    if(move_uploaded_file($_FILES['photo4']['tmp_name'], $target4)) 
{ 

//Tells you if its all ok 
//echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
//echo "Sorry, there was a problem uploading your image."; 
} 

//Writes the PDF to the server 
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target5)) 
{ 

//Tells you if its all ok 
//echo "The file ". basename($_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory"; 
} 
else { 

//Gives and error if its not 
//echo "Sorry, there was a problem uploading your image."; 
} 

    } 
    else { 
     echo '<div class="errormess"> ' 
      . 'Error code: '. intval($_FILES['uploaded_file']['error']). '</div>'; 
       echo '<script> 
alert("A file for PDF Specs must be chosen. If a file for PDF Specs was chosen, see error code at the bottom of the page."); 
</script>'; 
    } 

    // Close the mysql connection 
    //$conn->close(); 
} 


else { 
    //echo 'Error! A file was not sent!'; 
} 

謝謝你的任何幫助。所有的幫助表示讚賞。

+0

刪除用戶名和密碼到您的數據庫! –

+0

刪除此問題並更改您的用戶名和密碼儘快 –

回答

1

如果您正在使用MSSQL作爲標籤建議,然後:

UPDATE new_trailers1 SET (photo=NULL, photo1=NULL, photo2=NULL, photo3=NULL, photo4=NULL) WHERE (photo='',photo1='',photo2='',photo3='',photo4='') 

應該

UPDATE new_trailers1 SET photo=NULL, photo1=NULL, photo2=NULL, photo3=NULL, photo4=NULL WHERE photo='' AND photo1='' AND photo2='' AND photo3='' AND photo4='' 

這將只更新所有的照片都是空白,雖然。因此,如果您想根據每個字段的唯一值更新每個字段,則需要爲每個照片字段發佈四個單獨的更新語句。

+0

謝謝,代碼解決了我的問題。 – Kelsey

+0

邁克爾,我得到了幫助的投票嗎?有點感謝很長的路:) – Stuart

0

我認爲你可以從你的數據庫設置解決它。 嘗試轉到您的SQL表格,單擊「結構」,然後編輯並在必填字段中將NULL設置爲默認值。

+0

我沒有訪問cPanel,所以我不得不通過PHP來改變表。當我創建表格時,我使用PHP代碼'photo varchar(150)null,'在創建表格時創建'photo'列。我必須使用哪些PHP代碼來更改列以使其默認設置爲NULL? – Kelsey

相關問題