Hello,

I am trying to create a little bit tricky part I thought I would be sucessfull but it didn't happend. Okay so I created a form having multiple field with the same name like users adds package details so it depends on him how much he would be able to or like to add by adding the add new field so the sissue is coming up the fiedls or not inserting in the database

pkginsert.php

<?php
    require_once("connection.php");

    echo $pkgname   = $_POST["pkgname"];
    echo $pkgprice  = $_POST["pkgprice"];
    echo $mnthprice     = $_POST["mnthprice"];

    if(isset($_POST["submit"])) {
        echo $insert = mysqli_query($connection, "INSERT INTO packages (pkg_name, pkg_price, month_price) VALUES ('$pkname', '$pkgprice', '$mnthprice')");
        $pkg_id = mysqli_insert_id($connection); 

        foreach($_POST["pkg_feature"] as $pkg_f) {
            echo $data1 = mysqli_real_escape_string($connection, $pkg_f);

            echo $insert1 = mysqli_query($connection, "INSERT INTO pkg_detail (pkg_feature, pkg_id) VALUES ('$data1', '$pkg_id')");
        }

        foreach($_POST["pkg_detail"] as $pkg_d) {
            echo $data2 = mysqli_real_escape_string($connection, $pkg_d);

            echo $insert2 = mysqli_query($connection, "INSERT INTO pkg_detail (pkg_detail, pkg_id) VALUES ('$data2', '$pkg_id')");
        }

        header("Location: packages.php?id=".$_SESSION['id']);
    }
?>

newpkg.php

                <form method="POST" action="includes/pkginsert.php">
                <div class="form-group">
                    <label>Package Name</label>
                    <input class="form-control" name="pkgname">
                </div>

                <div class="form-group">
                    <label>Package Price</label>
                    <input class="form-control" name="pkgprice">
                </div>

                <div class="form-group">
                    <label>Monthly Price</label>
                    <input class="form-control" name="mnthprice">
                </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="text" name="pkg_feature[]" class="form-control alignleft" placeholder="Package Feature"/>&nbsp;<input type="text" name="pkg_detail[]" class="form-control alignleft" placeholder="Feature Detail" /><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">
                    <input type="text" name="pkg_feature[]" class="form-control alignleft" placeholder="Package Feature" />
                    <input type="text" name="pkg_detail[]" class="form-control alignleft" placeholder="Feature Detail" />
                    <div class="clear"></div>
                    </div>
                </div>

                <div class="form-group">
                    <input type="submit" class="btn btn-default" value="Submit Button" />
                    <input type="Reset" class="btn btn-default" value="Reset Value" />
                </div>
                </form>

so please let me know if anyone can help me out

So.. not gonna lie.. but your echo's really hurt my eyes and my head.. I don't understand their purpose...

for example, this line:

echo $insert = mysqli_query($connection, "INSERT INTO packages (pkg_name, pkg_price, month_price) VALUES ('$pkname', '$pkgprice', '$mnthprice')");

I read this to say "please output the result of the action of setting the variable $insert to the return value (in this case, true or false) of the function mysqli_query, which in turn takes connection and the query provided."

In no way are you going to output anything useful. $insert will always set a value, true or false, and therefore you will always echo true or, more likely, nothing at all.

but anyway...

because you are sending an array to the server, you will have to iterate the data and insert using a batch query, or a bunch of individual queries for each data set.

It may help you to, instead of echo, to use var_dump() on things like $_POST. In fact, I encourage you to var_dump($_POST); right before your include and see what is there so you can better understand what your data looks like.

If, for any reason, var_dump($_POST); is empty or does not contain the data you expect, you may need to look at your server configurations, or add encoding on your forms so the server knows what to do with the incoming data.

I also would think that echoing before doing a header redirect would cause an error, or at the very least confuse the heck out of the browser before the 301.. I dunno, YMMV.

Hope that helps you..

Ryan

I forgot to remove echo while posting the question this echo is used to check either the query and the values are posting it correctly or not

Hi There, There are quite a few issues with your code but i will attempt to fix them.

In your HTML / JS you should use proper naming in your control names, i have changed the [] in the names with a logical incremental integer value which will then loop through from 1-10 per control set.

e.g. : pkg_feature1 & pkg_detail1 - pkg_feature10 & pkg_detail10

`<form method="POST" action="includes/pkginsert.php">
            <div class="form-group">
                <label>Package Name</label>
                <input class="form-control" name="pkgname">
            </div>

            <div class="form-group">
                <label>Package Price</label>
                <input class="form-control" name="pkgprice">
            </div>

            <div class="form-group">
                <label>Monthly Price</label>
                <input class="form-control" name="mnthprice">
            </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="text" name="pkg_feature' + x + '" class="form-control alignleft" placeholder="Package Feature"/>&nbsp;<input type="text" name="pkg_detail' + x + '" class="form-control alignleft" placeholder="Feature Detail" /><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">
                    <input type="text" name="pkg_feature1" class="form-control alignleft" placeholder="Package Feature" />
                    <input type="text" name="pkg_detail1" class="form-control alignleft" placeholder="Feature Detail" />
                    <div class="clear"></div>
                </div>
            </div>

            <div class="form-group">
                <input type="submit" class="btn btn-default" value="Submit Button" />
                <input type="Reset" class="btn btn-default" value="Reset Value" />
            </div>
        </form>`

Secondsly, with the php page i have adjusted the code for various reasons.

  1. You cannot use echo and header("Location..."); n the same page, because header will not change if anything is written on the page.
  2. To adjust to the incremental integer

    <?php
    require_once("connection.php");
    //echo $pkgname = $_POST["pkgname"];
    //echo $pkgprice = $_POST["pkgprice"];
    //echo $mnthprice = $_POST["mnthprice"];
    /**echo $insert =*/ mysqli_query($connection, "INSERT INTO packages (pkg_name, pkg_price, month_price) VALUES ('$pkname', '$pkgprice', '$mnthprice')");
    $pkg_id = mysqli_insert_id($connection);

        for($i = 0; $i < 10; $i++){
            if(isset($_POST["pkg_feature$i"])){
                $data1 = mysqli_real_escape_string($connection, $_POST["pkg_feature$i"]);
                $data2 = mysqli_real_escape_string($connection, $_POST["pkg_detail$i"]);
                mysqli_query($connection, "INSERT INTO pkg_detail (pkg_feature, pkg_detail, pkg_id) VALUES ('$data1', '$data2', '$pkg_id')");
            }
        }
        header("Location: packages.php?id=".$_SESSION['id']);
    ?>
    

Also, im not sure why you had your code within the if(isset($_POST["submit"])) {} block, this seems unnneded.

There might be some additional issues with the code but this should send you into the right direction.

Hello no it worked the query is aplo inserting in the database but wait here comes my main issye

<?php
    session_start();
    require_once("connection.php");

    $pkgname    = $_POST["pkgname"];
    $pkgprice   = $_POST["pkgprice"];
    $mnthprice  = $_POST["mnthprice"];

    if(isset($_POST["submit"])) {
        $insert = mysqli_query($connection, "INSERT INTO packages (pkg_name, pkg_price, month_price) VALUES ('$pkgname', '$pkgprice', '$mnthprice')");
        $pkg_id      = mysqli_insert_id($connection); 

        foreach($_POST["pkg_feature"] as $pkg_f) {
            $data1 = mysqli_real_escape_string($connection, $pkg_f);            

            //I beleve this loop is not running as it inserting only the last value not the first or the othe one.
            foreach($_POST["pkg_detail"] as $pkg_d) {
                $data2 = mysqli_real_escape_string($connection, $pkg_d);
            }
            $insert1 = mysqli_query($connection, "INSERT INTO pkg_detail (pkg_detail, pkg_feature, pkg_id) VALUES ('$data2', '$data1', '$pkg_id')");
        }

        header("Location: ../packages.php?id=".$_SESSION['id']);
    }
?>

sorry your code didn't worked

Agreed that maybe the if block needs sorting, and please PLEASE put a real escape on those variables: (line 5) $pkgname = mysqli_real_escape_string ( $connection , $_POST["pkgname"] ). Additional validation would be a good idea, but isn't necessarily essential.

After line 16, add a new line with print_r($_POST["pkg_detail"]);, so we can see exactly what that loop is working with (assuming you're right in thinking that it's that loop not working).

Also, my fancy tip of the day: no need for the PHP closing tag (?>) in files where there is only PHP output (i.e. no HTML afterwards).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.