2017-05-08 95 views
0
<?php 

    if(isset($_POST['hour'])&&isset($_POST['day'])){ 

     //the path to store the uploaded image. 
     $target = "images/".basename($_FILES['image']['name']); 

     $db = mysqli_connect("localhost","root","","a_database"); 

     $image = $_FILES['image']['name']; 
     $carmodel = $_POST['carmodel']; 
     $cartype = $_POST['cartype']; 
     $hour = $_POST['hour']; 
     $day = $_POST['day']; 

     $query = "INSERT INTO `cardetails` (`image`, `carmodel`, `cartype`, `hour`, `day`) VALUES ('".$image."', '".$carmodel."', '".$cartype."', '".$hour."', '".$day."')"; 
     mysqli_query($db,$query);//stores the data 

     //move uploaded image into the folder. 
     if(move_uploaded_file($_FILES['image']['tmp_name'],$target)){ 
      header('Location:upload_success.php'); 
     }else{ 

      header('Location:upload_fail.php'); 
     } 

    }else{ 
      echo ' '; 
    } 

?> 


<div class="col-lg-offset-4"> 
    <form method="POST" action="upload.php" enctype="multipart/form-data"> 
    <input type="hidden" name="size" value="1000000"> 
    <div class="form-group row"> 
     <input type="file" name="image"> 
    </div> 

    <div class="form-group col-lg-4 row"> 
     <label> Car Model </label> 
     <select name="carmodel" class="form-control" > 
     <option value="Myvi">Myvi</option> 
     <option value="Vios">Vios</option> 
     <option value="Lambo">Lambo</option> 
     </select> 
    </div> 

    <div class="form-group row col-lg-offset-4"> 
    <label> Car Type </label> 
     <input type="radio" name="cartype" value="Auto">Auto 
     <input type="radio" name="cartype" value="Manual"> Manual 
    </div> 

    <div class="form-group"> 
     <input type="text" name="hour" placeholder="$/hour"> 
    </div> 

    <div class="form-group"> 
     <input type="text" name="day" placeholder="$/day"> 
    </div> 

    <div class="form-group"> 
     <button type="submit">Upload</button> 
    </div> 

</form> 

</div> 

我已經成功地將數據插入數據庫與圖像名稱,但圖像似乎並沒有上傳到我的圖像文件,有什麼可能是錯的?根據上面的代碼,調用upload_fail.php。如果用$ _POST ['image']設置,沒有任何反應。如何使用SQL將圖像存儲到文件中?

+1

你的腳本是[SQL注入攻擊]的風險(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql -injection-in-php) 看看發生了什麼事[Little Bobby Tables](http://bobby-tables.com/)即使 [if你是逃避投入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) 使用[編寫的參數化語句](http ://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly

+0

你的完整圖像路徑是什麼?你有沒有給755許可? – C2486

+0

如果發生錯誤,move_uploaded_file()將返回false,併發出警告。也許你應該看看你的Apache錯誤日誌,看看警告是什麼?或者打開錯誤輸出(你的php.ini中的display_errors = On),這樣你就可以直接看到輸出。 –

回答

0

我想,你沒有足夠的權限寫入images目錄。目錄本身是否存在images

如果你想知道什麼是錯的,你可以使錯誤在你的PHP文件一樣的開頭報道:

<?php 
    error_reporting(E_ALL); 
    ini_set('display_errors', 1); 
    ... 
相關問題