简体   繁体   中英

I want to add multiple images into MySQL database

I'm working on this feature in my project where I want to add three(3) images along side other details into my database row and I want this images to be in separate columns in one row.... So far below is my code. the code is not working yet... The images and texts are not uploading to the database.... Please help me out guys. What am I doing wrong. Thanks.

Below is my code sample:

//THE PHP SECTION//

<?php

session_start();
include 'includes/config.php';

if (isset($_POST['post']) && isset($_POST['itemtype'])) {

    $title = mysqli_real_escape_string($link, $_POST['title']);
    $itemtype = mysqli_real_escape_string($link, $_POST['itemtype']);
    $description = mysqli_real_escape_string($link, $_POST['description']);
    $image1 = $_FILES['image1']['name'];
    $image1_tmp_name = $_FILES['image1']['tmp_name'];
    $image2 = $_FILES['image2']['name'];
    $image2_tmp_name = $_FILES['image2']['tmp_name'];
    $image3 = $_FILES['image3']['name'];
    $image3_tmp_name = $_FILES['image3']['tmp_name'];
    $price = mysqli_real_escape_string($link, $_POST['price']);
    $category = mysqli_real_escape_string($link, $_POST['category']);
    $name = mysqli_real_escape_string($link, $_POST['name']);

    //image file directory
    $target1 = 'images/user_ads/'.basename($image1);
    $target2 = 'images/user_ads/'.basename($image2);
    $target3 = 'images/user_ads/'.basename($image3);

    if (move_uploaded_file($_FILES['image1_tmp_name'], $target1)) {

    }
    if (move_uploaded_file($_FILES['image2_tmp_name'], $target2)) {

    }
    if (move_uploaded_file($_FILES['image3_tmp_name'], $target3)) {

    }

    //insert into ost database
    $insert = "INSERT INTO products(Title,Product_Type,Description,Image1,Image2,Image3,Price,Category,Name,PostedDate)VALUES('$title','$itemtype','$description','$target1','$target2','$target3','$price','$category','$name',NOW())";
    $insertKwary = mysqli_query($link, $insert);

    if ($insertKwary) {
        $msg = "<div class='alert alert-danger alert-success'>Product Submitted</div>";
    }else{
        $msg = "<div class='alert alert-danger alert-success'>Product Not Submitted...Try again</div>";
    }
}

?>

//THE HTML SECTION//

<div class="col-md-8 col-md-offset-2">
                    <?php if(isset($msg)) { echo $msg; } ?>
                    <form action="" method="POST" enctype="multipart/form-data" class="postAdForm" id="postAdForm">
                        <div class="form-group">
                            <label for="Ad_title">Item Title</label>
                            <input type="text" name="title" class="form-control title" id="title" required=""/>
                        </div>
                        <div class="form-group">
                            <label for="itemtype">Item Type</label>
                            <select class="form-control" name="itemtype" id="itemtype">
                                <option>Sale</option>
                                <option>Request</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="description">Item Description</label>
                            <textarea name="description" class="form-control description" id="description" rows="7" required=""></textarea>
                        </div>
                        <div class="form-group">
                            <label for="price">First Image</label>
                            <input type="file" name="image1" class="form-control image1" id="image1" required="" />
                        </div>
                        <div class="form-group">
                            <label for="price">Second Image</label>
                            <input type="file" name="image2" class="form-control image2" id="image2" required="" />
                        </div>
                        <div class="form-group">
                            <label for="price">Third Image</label>
                            <input type="file" name="image3" class="form-control image3" id="image3" required="" />
                        </div>
                        <div class="form-group">
                            <label for="price">Price</label>
                            <input type="text" name="price" class="form-control price" id="price" required="" />
                        </div>
                        <div class="form-group">
                            <label for="category">Item Category</label>
                            <select class="form-control" name="category" id="category">
                                <option>Sale</option>
                                <option>Request</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="price">Name</label>
                            <input type="text" name="name" class="form-control name" id="name" required="" readonly="" />
                        </div>
                        <div class="form-group">
                            <input type="submit" name="post" class="btn btn-post post" id="post" value="POST AD" />
                        </div>
                    </form>
                </div>

I've also attached an image of the error i'm getting..

image of web page showing thee errors i get..error gotten

The error is due to these lines:

if (move_uploaded_file($_FILES['image1_tmp_name'], $target1)) {
}
if (move_uploaded_file($_FILES['image2_tmp_name'], $target2)) {
}
if (move_uploaded_file($_FILES['image3_tmp_name'], $target3)) {
}

You're setting $image1_tmp_name, not $_FILES['image1_tmp_name'].

Try this:

if (move_uploaded_file($image1_tmp_name, $target1)) {
}
if (move_uploaded_file($image2_tmp_name, $target2)) {
}
if (move_uploaded_file($image3_tmp_name, $target3)) {
}

Edit: Using mysqli_error() to print the error helped solve additional issues (Mentioned in comments)

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