Search code examples
phphtmlmysqlwordpresswordpress-shortcode

How to set same id for multiple rows in a table


I have a table named wp_shortcode. When a form is data from the form will be inserted to multiple rows into the table. Below given is my form:

<form method="POST" action="" class="form1">
    <input type="submit" name="submit" value="Generate"><br>
    Post Type
    <select class="taxonomy">
        <option value="">Select</option>
    </select>
    <br>
    Category
    <select class="cat">
        <option value="">Select</option>
    </select>
    <h3>Questions</h3>
    <button class="new_btn">Add New</button><br>
    <div class="add_ques">
        <span>Question1</span>
        <textarea name="ques1" id="ques1"></textarea>
        <br>
        Option1
        <select class="option" name="opt1" id="opt1">
            <option value="1">1</option>
            <option value="2">2</option>
        </select>
        <br>
    </div>
</form>

The add_ques div section will be multiplied on add new button click.I need to insert the data from the form to wp_shortcode table.Currently the table have s_id,post_types,category,question,option columns.

How can i add a common id to the rows generated during form submission.


Solution

  • Here is the solution: I add an addition column group_id to the table. And when we submit the form we will check group_id values:

    $post_id = $wpdb->get_results("SELECT group_id FROM $table_name");
    $siz = sizeof($post_id);
    $index = $siz-1;
    $gid = $post_id[$index];
    $group = $gid->group_id;
    $group_id = (int)$group; 
    

    And last group_id value is checked:

    if($group_id == 0){
        $id = 1;
    }
    else{
        $id = $group_id+1;
    }
    

    This id is inserted along with other values into table. By this set of data inserted during of form submission will get same id.