0

I am trying to create an AdLister site - and so far everything a user posts into the database displays properly on the site except an image. Below is my code - Please let me know if I left anything out:

    $insert_table = "INSERT INTO posts (userid, post_date, title, price, description, email, location, image) VALUES (:userid, :post_date, :title, :price, :description, :email, :location, :image)";
    $stmt = $dbc->prepare($insert_table);
    $stmt->bindValue(':userid', 1, PDO::PARAM_STR);
    $stmt->bindValue(':post_date', $date, PDO::PARAM_STR);
    $stmt->bindValue(':title', $title, PDO::PARAM_STR);
    $stmt->bindValue(':price', $price, PDO::PARAM_STR);
    $stmt->bindValue(':description', $description, PDO::PARAM_STR);
    $stmt->bindValue(':email', $email, PDO::PARAM_STR);
    $stmt->bindValue(':location', $location, PDO::PARAM_STR);
    $stmt->bindValue(':image', $image, PDO::PARAM_STR);

    $stmt->execute();

    return $errors;
    }

    if (!empty($_POST)) {
        if (checkValues()) {

        $errors = insertPost($dbc);         
    } else {
        $message = "Invalid format. Please try again.";
        $javascript = "<script type='text/javascript'>alert('$message');</script>";
        echo $javascript;
    }
}

    if(Input::has('title')){
        if($_FILES) {
            $uploads_directory = '/img';
            $filename = $uploads_directory . basename($_FILES['image']['name']);
            if (move_uploaded_file($_FILES['image']['tmp_name'], $filename)) {
                // echo '<p>The file '. basename( $_FILES['image']['name']). ' has been uploaded.</p>';
            } else {
                //alert("Sorry, there was an error uploading your file.");
            }
        }
       }
        <table class="table table-hover table-bordered table-striped">
        <tr class='table-hover'>
            <th class="header">Photo</th>
            <th class="header col-md-1">Date Posted</th>
            <th class="header">Title</th>
            <th class="header col-md-1">Price</th>
            <th class="header col-md-6">Description</th>
            <th class="header col-md-6">Image</th>
        </tr>

            <?php
            foreach ($posts as $post):?>
                <tr class='table table-hover table-bordered body'>
                    <td>Photo</td>
                    <td><?= $post['post_date'] ?></td>
                    <td><?= $post['title']?></td> 
                    <td><?= $post['price']?></td>
                    <td><?= $post['description']?></td>
                    <td><?= $post['image']?></td>
            <?php endforeach ?>
            </tr>
</table>
1

1 Answer 1

1

I'm guessing your trying to insert the image as a posted string value (like the other inputs) except data received from file inputs is stored as an array called $_FILES

So...

$_POST['image']

Will be empty but

$_FILES['image']['name']

Will contain the actual filename.

Rearrange your code to upload the file and get the name, then use that value to insert.

Oh and don't forget to include:

enctype="multipart/form-data"

on your html form tag!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.