tejas
tejas

Reputation: 27

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

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"> Select Course Name</option>
                                    <?php
                                                $sql = "SELECT * from course_master";
                                                $result = $connect->query($sql); 
                                                 while($row_pt = $result->fetch_array()) 
                                                     { 

                                                         ?>
                                                  <option value="<?php echo $row_pt['course_id']; ?>"><?php echo $row_pt['course_name']; ?></option>

                                                <?php 
                                                     }
                                     ?>
                         </select >
                     </td>


                    <td class="col-md-2"> <input class="form-control topic_name" id="topic_name1" name="topic_name[]" maxlength="250"  value="" placeholder="Enter Topic Name" type="text" ></td>

                    <td class="col-md-4"><textarea class="form-control" style="resize: none;" id="topic_description1"  name="topic_description[]" rows="3" placeholder="Enter Description"></textarea></td>

                    <td class="col-md-2"><select class="form-control select2" id="parent_id_1" name="parent_id[]" data-live-search="true" ></select></td>

                    <td class="col-md-2"><input type="button" name="add" value="Add Row" class="btn btn-success"></td>

               </tr>  
        </table>
    </div>

    <!-- /.box-body -->
    <div class="box-footer">
      <button type="submit" name="submitmultiple" id="button" class="btn btn-primary btn-form-action btn-submit">Save</button>
    </div>
    </form>

Upvotes: 1

Views: 2140

Answers (2)

Pervaiz Iqbal
Pervaiz Iqbal

Reputation: 326

1) your question is not clear - dont understand how sending multiple cources

add multiple here

select

to

select multiple

2) add single quotes along fields (course_id,topic_name,topic_description,parent_id) to ('course_id','topic_name','topic_description','parent_id')

after changing these few changes i can see this query by submitting form

INSERT INTO syllabus ('course_id','topic_name','topic_description','parent_id') VALUES ('c2','asd','','1')

query seems fine - should insert data

Upvotes: 1

Valerian Pereira
Valerian Pereira

Reputation: 727

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";
            }
        }
    }
}

Upvotes: 1

Related Questions