0

Hello,

Little tricky things I am trying to create want to add more then 1 image in the database admin can create a gallery but for that he can add mor then 1 image as in gallery he can add more then 1 image dont know how to explain but here you can view my codes

posts.php

function add_posts($connect) {

        $imgname = $_FILES["ft_img"]["name"];
        $imgtype = $_FILES["ft_img"]["type"];
        $imgtemp = $_FILES["ft_img"]["tmp_name"];

        echo $path = "./img/featured/".$imgname;

        $title   = $_POST["descrip"];
        $descrip   = mysqli_real_escape_string($connect, $_POST["descrip"]);
        $android   = $_POST["android"];
        $iphone    = $_POST["iphone"];
        $cbtn_text = $_POST["cbtn_text"];
        $cbtn_url  = $_POST["cbtn_url"];

        move_uploaded_file($imgtemp, $path);

        for($i = 0; $i < 20; $i++){
            if(isset($_POST["gallery_img$i"])) {
                $imggall = $_FILES["gallery_img"]["name"];
                $imgtype = $_FILES["gallery_img"]["type"];
                $imgtemp = $_FILES["gallery_img"]["tmp_name"];

                echo $path = "./img/gallery/".$imgname;

                move_uploaded_file($imgtemp, $path);
                mysqli_query($connection, "INSERT INTO gallery (image) VALUES ('$imggall')");
            }
        }   

        $query_insert = "INSERT INTO posts(title, description, button1_text, button1_url, button2_text, button3_text, f_img) VALUES ('$title', '$descrip', '$cbtn_text', '$cbtn_url', '$android', '$iphone')";
        $confirm      = mysqli_query($connect, $query_insert);

        if($confirm) {
            $_SESSION["message"] = "Logo updated successfully"; 
            header("Location: post.php?id=".$_SESSION["id"]);   
        } else {        
            $_SESSION["message"] = "There was some errors please re-enter your information";
            header("Location: post.php?id=".$_SESSION["id"]);
        }
    }

post_form.php

<p>
                <form method="POST" action="post.php?id=<?php echo $_SESSION["id"]; ?>" enctype="multipart/form-data">

                <div class="form-group">
                    <label>Title</label>
                    <input class="form-control" name="title">
                </div>

                <div class="form-group">
                    <label>Category</label>
                    <div class="checkbox">
                        <?php
                            $query = mysqli_query($connection, "SELECT DISTINCT cat_name FROM category");
                            while($row = mysqli_fetch_assoc($query)) {
                                if(!empty($row["cat_name"])) {
                        ?>

                        <label><input type="checkbox" value="<?php echo $row["cat_name"]; ?>"><?php echo $row["cat_name"]; ?></label>

                        <?php } } ?>

                        <?php
                            $query1 = mysqli_query($connection, "SELECT DISTINCT parent FROM category");
                            while($row1 = mysqli_fetch_assoc($query1)) {
                                if($row1["parent"] !== "None") {
                        ?>

                        <label><input type="checkbox" value="<?php echo $row1["parent"]; ?>"><?php echo $row1["parent"]; ?></label>

                        <?php } } ?>
                    </div>
                </div>


                <div class="form-group">
                    <label>Featured Image</label>
                    <input type="file" name="ft_img">
                </div>

                <div class="form-group">
                    <label>Description</label>
                    <textarea class="form-control" name="descrip" rows="6"></textarea>
                </div>

                <div class="form-group">
                    <label>Android Button</label>
                    <input class="form-control" name="android">
                </div>

                <div class="form-group">
                    <label>Iphone Button</label>
                    <input class="form-control" name="iphone">
                </div>

                <div class="form-group">
                    <label>Custom Button Text</label>
                    <input class="form-control" name="cbtn_text">
                </div>

                <div class="form-group">
                    <label>Custom Button Link</label>
                    <input class="form-control" name="cbtn_url">
                </div>

                <script type="text/javascript">
                $(document).ready(function() {
                    var max_fields      = 10; //maximum input boxes allowed
                    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
                    var add_button      = $(".add_field_button"); //Add button ID

                    var x = 1; //initlal text box count
                    $(add_button).click(function(e){ //on add input button click
                        e.preventDefault();
                        if(x < max_fields){ //max input box allowed
                            x++; //text box increment
                            $(wrapper).append('<div class="form-group"><input type="file" name="gallery_img[]" style="float:left;" /><a href="#" class="remove_field"><i class="fa fa-times"></i></a><div class="clear"></div></div>'); //add input box
                        }
                    });

                    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
                        e.preventDefault(); $(this).parent('div').remove(); x--;
                    })
                });

                </script>

                <div class="input_fields_wrap form-group">
                    <div class="form-group">
                    <button class="add_field_button btn btn-default">Add More Fields</button>
                    </div>

                    <div class="form-group">
                    <label>Gallery Images</label>
                    <input type="file" name="gallery_img[]">
                    <div class="clear"></div>
                    </div>
                </div>

                <div class="form-group">
                    <input type="submit" class="btn btn-default" value="Submit" name="posts" />
                    <input type="Reset" class="btn btn-default" value="Reset Value" />
                    <a href="cat.php?id=<?php echo $_SESSION['id'];?>" class="btn btn-danger">Cancel</a>
                </div>

                </form>
                </p>

Edited by UK-1991: wrong form field name

2
Contributors
2
Replies
19
Views
2 Years
Discussion Span
Last Post by UK-1991
0

You're looping the $_POST array, instead you have to loop the $_FILES array. Follow the documentation and the comments at this page:

If still in doubt create a dummy upload script and place this:

<?php

    echo "<p>$_POST array:</p>";
    echo "<pre>" . print_r($_POST) . "</pre>";

    echo "<p>$_FILES array:</p>";
    echo "<pre>" . print_r($_FILES) . "</pre>";

so you can see what is generated by your upload form.

0

I updated my code with a new concept and got this result

Array
(
    [0] => fg_4.jpg
    [1] => fg_5.jpg
)
Array
(
    [0] => fg_4.jpg
    [1] => fg_5.jpg
)

This is my updated code:

function add_posts($connect) {

        $imgname = $_FILES["ft_img"]["name"];
        $imgtype = $_FILES["ft_img"]["type"];
        $imgtemp = $_FILES["ft_img"]["tmp_name"];

        $path = "./img/featured/".$imgname;

        $title   = $_POST["descrip"];
        $descrip   = mysqli_real_escape_string($connect, $_POST["descrip"]);
        $android   = $_POST["android"];
        $iphone    = $_POST["iphone"];
        $cbtn_text = $_POST["cbtn_text"];
        $cbtn_url  = $_POST["cbtn_url"];

        move_uploaded_file($imgtemp, $path);

        $imggall = $_FILES["gallery_img"]["name"];
        $imgtype = $_FILES["gallery_img"]["type"];
        $imgtemp = $_FILES["gallery_img"]["tmp_name"];

        $count = count($imggall);
        for($i = 0; $i < $count; $i++){



                print_r($imggall);
                /*$path = "./img/gallery/".$imggall;
                $len = count($image);
                move_uploaded_file($imgtemp, $path);
                $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imggall')");*/
        }   
      }
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.