Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

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

Course Queries Syllabus Queries
Max. 2000 characters
Replies

usr_profile.png
Brian Burl

User

( 5 months 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

<form  id="submitMultipleData" class="submitMultipleData"  role="form" name="hl_form" method="post" >
        <div class="box-body">
          <div class="row">
          </div>
          <!-- /.row -->

          <table class="table table-bordered table-striped table-condensed table-hover" id="dynamic_field">  
               <th>Course Name</th>
               <th>Topic Name</th>
               <th>Description</th>
               <th>Parent Topic Name</th>
               <th>Action</th>

               <tr id="id" class="trrow"> 
                    <td class="col-md-4">
                        <select class="form-control select2 selectCourse" data-id="1"  id="course_id_1" name="course_id[]" data-live-search="true" >
                                <option value="" selected="selected"
								 								 
							usr_profile.png
							
Garry Buttler

User

( 5 months 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";
            }
        }
    }
}

what's your interest