How to insert HTML Form array into database rows in mySQL with PHP

Course Queries Syllabus Queries 3 years ago

4.85K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago


Insert HTML Form array into database rows in mySQL with PHP i am inserting html array into database using for and foreach problem inserting data in to database using for loop its inserting blank data please help. please give advice on it or give solution thank you in advance i have tried the below code or any different method using for it.

php file

 if(isset($_POST['submitmultiple']))
{

    $course_id = $_POST['course_id'];
    $topic_name = $_POST['topic_name'];
    $topic_description = $_POST['topic_description'];
    $parent_id = $_POST['parent_id'];


    echo count($course_id);

    if(!empty($course_id))
    {
        for($i = 0; $i < count($course_id); $i++)
        {
            /*if(!empty($course_id[$i]))
            {*/ 
            foreach ($_POST['course_id'] as $value) 
            { 
                $course_id = $course_id[$i];
                $topic_name = $topic_name[$i];
                $topic_description = $topic_description[$i];
                $parent_id = $parent_id[$i];

                $sql = "INSERT INTO syllabus (course_id,topic_name,topic_description,parent_id) VALUES ('$course_id','$topic_name','$course_description','$parent_id')";
                if($connect->query($sql) === TRUE) 
                {
                    $valid[1] = "Added Successfully";   
                } 
                else 
                {
                    $valid[2] = "Error while Inserting";
                }
            }
            /*}*/
        }
    }
}

html code

  id="submitMultipleData" class="submitMultipleData"  role="form" name="hl_form" method="post" >
         class="box-body">
           class="row">
          
<table class="table table-bordered table-striped table-condensed table-hover" id="dynamic_field"> Course Name Topic Name Description Parent Topic Name Action id="id" class="trrow"> class="col-md-4"> class="form-control select2 selectCourse" data-id="1" id="course_id_1" name="course_id[]" data-live-search="true" > value="" selected="selected"
0 views
0 shares

profilepic.png
manpreet 3 years ago

Assuming the value at indexes is related for course/topic/parent, etc. You can skip one of the for loops too.

if(isset($_POST['submitmultiple']))
{

    $course_id = $_POST['course_id'];
    $topic_name = $_POST['topic_name'];
    $topic_description = $_POST['topic_description'];
    $parent_id = $_POST['parent_id'];

    echo count($course_id);

    if(!empty($course_id))
    {
        foreach ($_POST['course_id'] as $key => $value) 
        { 
            $tempcourse_id = $course_id[$key];
            $temptopic_name = $topic_name[$key];
            $temptopic_description = $topic_description[$key];
            $tempparent_id = $parent_id[$key];

            $sql = "INSERT INTO syllabus (course_id,topic_name,topic_description,parent_id) VALUES ('$tempcourse_id','$temptopic_name','$temptopic_description','$tempparent_id')";

            //Verify the query formed
            echo $sql."\n";
            if($connect->query($sql) === TRUE) 
            {
                $valid[1] = "Added Successfully";   
            } 
            else 
            {
                $valid[2] = "Error while Inserting";
            }
        }
    }
}

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum