简体   繁体   中英

Php is uploading images but not inserting in database

I'm developing a back end for my blog but the code to upload images isn't working as expected. When i submit it uploads the image and moves it to the designated folder but in the database there is no record inserted. Here is the php for uploading

<?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>";

  }


?>

And here is the form

<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> 

Replace

$fileToUpload=$_POST['fileToUpload'];

with

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

Try this

$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";
}

and my opinion is add the insert code after move_uploaded_file get success

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM