0

Alright I have way to much time invested in this. I am new to PHP programming and trying to grasp the basics, but I am a little lost as of last night I was able to get a PHP form to upload basic data like a name address and stuff to my (MySQ - PHPL) server.

But today I said let's do the next step which would be an image to the server. I have watched 3 videos on YouTube probably a 100 times trying it in so many different ways and still haven't been able to get it.

I'm trying to update the details of my barangay including its logo. Updating other details were successful but uploading the image file, updating the b_logo field as well isn't working.

Here's my code,

if(isset($_POST['updateBarangay'])){
$b_barangay=$_POST['b_barangay'];
$b_city=$_POST['b_city'];
$b_province=$_POST['b_province'];
$b_zip=$_POST['b_zip'];
$b_id=$_POST['b_id'];
$file_name = $_FILES['file']['name'];
$file_type = $_FILES['file']['type'];
$file_size = $_FILES['file']['size'];
$file_loc = $_FILES['file']['tmp_name'];
$file_store = "img/".$file_name;
move_uploaded_file($file_loc, $file_store);

$sql = "UPDATE b_details SET b_barangay=?, b_city=?, b_province=?, b_zip=?, b_logo=? WHERE b_id=?";
$stmt = $con->prepare($sql);
$stmt->bind_param("ssssss", $b_barangay, $b_city, $b_province, $b_zip, $file_name ,$b_id);
$stmt->execute();

$action = "Barangay Config | Updated Barangay Details";
$date= date("Y-m-d");
$time= date("h:i:sa");

$sqlinsert = "INSERT INTO tbl_logs (logs_name, logs_action, logs_position, logs_username, logs_date, logs_time) VALUES (?, ?, ?, ?, ?, ?)";
$stmtinsert = $con->prepare($sqlinsert);
$stmtinsert->bind_param("ssssss",  $_SESSION['user_fullname'], $action,  $_SESSION['user_position'], $_SESSION['user_username'], $date, $time);
$stmtinsert->execute();

if(! $stmt) {
    echo "error";
}else{
    header("location: config");
}}

By running this code, I was able to update the other details except b_logo. I'm trying to get the file name then upload it into a folder. Here's what it looks like after running the code, Database Output

Here's my HTML Code supporting the PHP code:

<div class="col-md-6">
      <div class="tile">
        <h3 class="tile-title">Barangay Details</h3>
        <div class="tile-body">
        <?php 
            $sql = "SELECT * FROM b_details";
            $result = $con->query($sql) ;
            while($row = $result->fetch_assoc()) { ?>
          <form class="form-horizontal" action="controller" method="POST" onsubmit="return confirm('Proceed updating the details of Barangay?')">
            <div class="form-group row">
              <label class="control-label col-md-3">Barangay:</label>
              <div class="col-md-8">
                <input class="form-control" type="text" name="b_barangay" value="<?php echo $row['b_barangay']; ?>" required>
              </div>
            </div>
            <div class="form-group row">
              <label class="control-label col-md-3">City:</label>
              <div class="col-md-8">
                 <input class="form-control" type="text" name="b_city" value="<?php echo $row['b_city']; ?>" required>  
              </div>
            </div>
            <div class="form-group row">
              <label class="control-label col-md-3">Province: </label>
              <div class="col-md-8">
              <input class="form-control" type="text" name="b_province" value="<?php echo $row['b_province']; ?>" required>
            </div>
            </div>
            <div class="form-group row">
              <label class="control-label col-md-3">ZIP Code: </label>
              <div class="col-md-8">
              <input class="form-control" type="text" name="b_zip" value="<?php echo $row['b_zip']; ?>" required>
              </div>
            </div>
            <div class="form-group row">
              <label class="control-label col-md-3">Barangay Logo:</label>
              <div class="col-md-8">
                <input type="file" name="file"/>
                
              </div>
            </div>
            <div class="form-group row">
              <div class="col-md-8 col-md-offset-3">
              </div>
            </div>
        </div>
        <div class="tile-footer">
          <div class="row">
            <div class="col-md-8 col-md-offset-3">
            <input type="hidden" name="b_id" value="<?php echo $row['b_id']; ?>">
              <button class="btn btn-success" type="submit" name="updateBarangay"><i class="fa fa-fw fa-lg fa-check-circle"></i>Update</button>
            </div>
            </form>
            <?php } ?>
          </div>
        </div>
      </div>
    </div>
    <div class="clearix"></div>

Your help would be much appreciated! Thank you so much!

  • Looks like the file itself isn't being uploaded. Take a look at the linked duplicate to see how to modify the `
    ` element to include file data.
    – David Nov 02 '20 at 14:31
  • @David I just solve it now, the answer was putting enctype='multipart/form-data' in
    . For whoever tag this a similar question, Thank you!
    – Abegail Grace Ulanday Nov 02 '20 at 14:33

0 Answers0