2015-11-09 37 views
1

我正在爲我的博客開發後端,但上傳圖片的代碼未按預期工作。 當我提交它上傳圖像,並將其移動到指定的文件夾,但在數據庫中沒有插入記錄。下面是上傳Php正在上傳圖片但未插入數據庫中

<?php 
session_start(); 
include('Connections/conn.php'); 
    if (!isset($_SESSION['userid'])) { 
    header("location:index.php"); 
    } 

    $suc=" "; 

    $writer=$_SESSION['my_username'];  

if(isset($_POST['submit'])) 

{ 
    error_reporting(E_ALL^E_NOTICE); 
    $target_dir = "uploads/"; 
    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
    $uploadOk = 1; 
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 

// Check if image file is a actual image or fake image 

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     echo "File is not an image."; 
     $uploadOk = 0; 
    } 

// Check if file already exists 
if (file_exists($target_file)) { 
    echo "Sorry, file already exists."; 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    echo "Sorry, your file is too large."; 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" 
&& $imageFileType != "gif") { 
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed."; 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    echo "Sorry, your file was not uploaded."; 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     echo "Sorry, there was an error uploading your file."; 
    } 
} 

    $title=$_POST['title']; 

    $intro=$_POST['intro']; 
    $body=$_POST['body']; 
    $keywords=$_POST['keywords']; 
    $date=$_POST['date']; 
    $fileToUpload=$_POST['fileToUpload']; 


    $sql2="Insert into articles(title,intro,body,keywords,date,writer,fileToUpload)VALUES('$title','$intro','$body','$keywords','$date','$writer','$target_file')"or die(mysqli_error()); 
    $result2 = mysqli_query($db_conn, $sql2); 

    $suc=" <div class='alert alert-success'> 
      <span><b>Success</b>: New article posted successfully!</span> 

     </div>"; 

    } 


?> 

的PHP和這裏是形式

<form method="POST" action"<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data"> 
<div class="form-group"> 
    <label>Title</label> 
    <input type="text" id="title" name="title" class="form-control" required/> 
</div> 
&nbsp; 
<div class="form-group"> 
    <label>Intro</label> 
    <textarea id="intro" name="intro" class="form-control" required></textarea> 
</div> 
<div class="form-group"> 
<label>Enter keyword tags</label><br/> 
    <div> 
     <input type="text" id="keywords" name="keywords" class="tagsinput" required/> 
    </div> 
</div> 

<div class="form-group">           
<label>Date</label><br/> 
<div> 
    <div class="input-group"> 
     <span class="input-group-addon"><span class="fa fa-calendar"></span></span> 
     <input type="text" id="date" name="date" class="form-control datepicker" placeholder="Select Date" required>            
    </div> 
    <span class="help-block">Click on input field to select date</span> 
</div>&nbsp; 

<div class="form-group"> 
    <div> 
     <label>Picture</label> 
     <input type="file" multiple class="file" data-preview-file-type="any" name="fileToUpload" id="fileToUpload"/> 
    </div> 
</div> 
<div> 

<div class="block"> 


<p>Type the content below, note that you can extend the height of the editor by dragging the bottom border.</p> 
<textarea class="summernote" id="body" name="body" placeholder="Enter the body text" required> 


</textarea> 
</div> 

    </div> 
</div>&nbsp;         



    <div class="form-group">           
     <div class="col-md-6"> 
      <button class="btn btn-danger btn-block" type="reset">Reset</button> 
     </div> 
     <div class="col-md-6"> 
      <button class="btn btn-info btn-block" type="submit" name="submit" value="submit">Publish Article</button> 
     </div>           
    </div>          
</form> 
+0

沒有與插入其他內容,只上傳圖片時沒有問題。 – HelpMeOut

+1

你應該嘗試@ Abdulla的回答 –

回答

0

更換

$fileToUpload=$_POST['fileToUpload']; 

$fileToUpload=$_FILES['fileToUpload']['name']; 
+0

只在數據庫中插入文件的名稱,但不上傳圖像 – HelpMeOut

+0

這就是你在這裏所要求的。休息之前爲你工作 –

0

試試這個

$sql2="INSERT INTO articles(title,intro,body,keywords,date,writer,fileToUpload)VALUES('$title','$intro','$body','$keywords','$date','$writer','$target_file')"); 

if (!mysqli_query($db_conn, sql2)) 
{ 
    echo("Error description: " . mysqli_error($db_conn)); 
} 
else{ 
    echo "Inserted"; 
} 

我的看法是添加插入代碼後move_uploaded_file獲得成功

相關問題