thedigicraft / Atom.CMS

Atom.CMS
56 stars 52 forks source link

Part 77 - Adding the Filename to the User Table Issue #156

Closed webstersgr closed 8 years ago

webstersgr commented 8 years ago

Hello there and congrats for this awesome tutorial (btw I'd like to donate some bucks as soon as complete the series). I have an issue with uploading and inserting the image in the database. I'm following your tutorial step by step and I'm working on a live server. I've went through this piece of code over and over and I can't find where I'm doing wrong, so I hope maybe you could help! This is what firebug shows me : screen shot 2016-01-21 at 20 25 11

I'm also pasting you the code from users.php & uploads.php ,hoping you can help me out! Thanks in advance!!!

uploads.php

<?php

include('../config/connection.php');

$ds = DIRECTORY_SEPARATOR;
id = $_GET['id'];

$storeFolder = '../uploads';

$ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$newname = time();
$random = rand(100,999);
$name = $newname.$random.'.'.$ext;

$q = "UPDATE users SET avatar = '$name' WHERE id = $id"; 
$r = mysqli_query($dbc, $q);

echo $q.'<br>';
echo mysqli_error($dbc);

if (!empty($_FILES)) {

    $tempFile = $_FILES ['file'] ['tmp_name'];

    $targetPath = dirname( __FILE__ ) . $ds. $storeFolder . $ds;

    $targetFile = $targetPath. $name;

    move_uploaded_file($tempFile,$targetFile);

}

?>

users.php

(I'm leaving out the pieces of the code inside the form tags to save some space)

<script>

    $ (document).ready(function() {

        Dropzone.autoDiscover = false;

        var myDropzone = new Dropzone("#avatar-dropzone");

    });

</script>

<h1>Users Manager</h1>

<div class="row">

    <div class="col-md-3">

        <div class="list-group">

            <a class="list-group-item" href="?page=users">
                    <h4 class="list-group-item-heading"><i class="fa fa-plus"></i> Add New User</h4>
            </a>

                <?php

                    $q = "SELECT * FROM users ORDER BY last ASC";
                    $r = mysqli_query($dbc, $q);

                        while ($list = mysqli_fetch_assoc($r)) {

                        $list = data_user($dbc,$list['id']);
                        //$blurb = substr(strip_tags($list['body']), 0, 100);

                        ?>

                        <a class="list-group-item <?php selected($list['id'], $opened['id'], 'active') ?>" href="index.php?page=users&id=<?php echo $list['id']; ?>">
                            <h4 class="list-group-item-heading"><?php echo $list['fullname']; ?></h4>
                            <!--<p class="list-group-item-text"><?php //echo $blurb; ?></p>-->
                        </a>

                <?php } ?>

        </div>

    </div>

    <div class="col-md-9">

        <?php if(isset($message)) {echo $message;}?>

        <form action="index.php?page=users&id=<?php echo $opened['id']; ?>" method="post" role="form" >

            ...................

        </form>

        <form action="uploads.php?id=<?php echo $opened['id']; ?>" class="dropzone" id="avatar-dropzone">

            <input type="file" name="file">

        </form>

    </div>
</div>
creptor commented 8 years ago

I'll try to help you has soon as I can, please be patient. -Just have to do something from 5 - 9 :c I'm not really into the uploads problems, but I'll do my best. Also I need the form data, not only the list. (that's the list not the user editor)

creptor commented 8 years ago

A no problem, I found the error....... The image shows POST http:/......, and in the code you have the $_GET function :D look here id = $_GET['id'];. Every $_GET should be $_POST.

webstersgr commented 8 years ago

Hello again and thank you so much for your quick response! I've tried to replace the $_GETwith $_POST in the id = $_GET['id']; but in this case it seems like the id isn't loaded (my firebug report shows this: "NetworkError: 500 Internal Server Error - http://websters.gr/WiseCMS/admin/uploads.php?id=".

While I'm trying to find out what's wrong, I tried to isolate somehow the problem and it seems that if I remove from uploads.php the lines of code for the storage in the database and from users.php the ?id=<?php echo $opened['id']; ?>, then the upload is successfully completed. So, as far as I can tell, the problem is in adding the file in the database's users table... When I'm adding this code the whole thing fails and I don't even get the image in the uploads folder anymore!

I can post you any file you may need, I just skipped the form data to avoid a long post. Thanks again for your time!!

webstersgr commented 8 years ago

@creptor Thank you so so much for your time. I've found the error! It was a stupid typo. I've forgotten the $ before id = $_GET['id']; I'm sorry for the trouble I've caused! I'm closing the issue and thanks again!!!

creptor commented 8 years ago

daaaam, I was so close :c nice catch.