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

Course Queries Syllabus Queries 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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">
          
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 2 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.

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community