-1

I have form inputs inside a table row which gets cloned on add new row button. I am trying to save all the rows in MySQL on clicking save button.

upon sending it through ajax to php I ony get only one value which is the default value before cloning.

The hmtl code:

<table role="table" class="striped table_view rowfy" id="user_table"> 
    <tbody role="rowgroup">
    <tr id="template" role="row"> 

    <td role="cell">
    <select name="module" class="browser-default mselect select module">
    <option value="" class="validate" disabled selected>Select User Menu Module</option>  
    <?php echo modules(); ?>
    </select>
    </td>
    <td role="cell">
    <select name="create[]" class="browser-default mselect select create">
    <option value="" class="validate" disabled selected>Create</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>
    </td>
    <td role="cell">
    <select name="edit[]" class="browser-default mselect select edit">
    <option value="" class="validate" disabled selected>Edit</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>
    </td>
    <td role="cell">
    <select name="delete[]" class="browser-default mselect select delete">
    <option value=""  disabled selected>Delete</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>
    </td>
    <td role="cell">
    <select name="view[]" class="browser-default mselect select view">
    <option value=""  disabled selected>View</option>
    <option value="yes">No</option>
    <option value="no">Yes</option>
    </select>

    </td>

    </tr>
    </tbody>
    </table>

jquery:

$(document).ready(function() {
    var template = $('#template'),
        id = 0;

$("#add-line").click(function() {
    if(!template.is(':visible'))
    {
        template.show();
        return;
    }
    var row = template.clone().append('<td><button class="btn btn-small btn btn-floating '
  + ($(this).is(":last-child") ?
    'rowfy-addrow remove red">-' :
    'rowfy-deleterow remove waves-effect waves-light red">-') 
  +'</button></td>');
    //template.find(".mselect").val("");
    row.attr('id', 'row_' + (++id));
    template.after(row);
});

$('.form-fields').on('click', '.remove', function(){
    var row = $(this).closest('tr');
    if(row.attr('id') == 'template')
    {
        row.hide();
    }
    else
    {
        row.remove();
    }
});



});

php code:

if(!empty($_POST['module'])){
$rowCount = count($_POST['module']);

for($i = 0; $i < $rowCount; $i++)
{       
$module = clean($_POST['module'][$i]);
$edit = clean($_POST['edit'][$i]);
$view = clean($_POST['view'][$i]);
$create = clean($_POST['create'][$i]);
$del = clean($_POST['delete'][$i]);

echo $rowCount;   
$sql = "INSERT INTO role_rights(rr_rolecode,rr_modulecode,rr_create,rr_edit,rr_delete,rr_view) 
VALUES('$role', '$module','$create','$edit','$del','$view')";

$suc =  mysqli_query($mysqli, $sql);  

}
}

Below is the header format of data being sent:

module: PAGES
create[]: yes
edit[]: no
delete[]: yes
view[]: no
module: CATEGORIES
create[]: no
edit[]: no
delete[]: yes
view[]: no
module: ATTRIBUTES
create[]: no
edit[]: no
delete[]: no
view[]: no
  • @Dharman I am not open to SQL injection. the purpose of **clean()** function is to take care of SQL Injection – Olajumoke Ademilade Jul 16 '19 at 22:57
  • There is no such function `clean` in PHP and it is doubtful it would ever protect you from SQL injection. [How can I prevent SQL injection in PHP?](https://stackoverflow.com/q/60174/1839439) – Dharman Jul 16 '19 at 23:02
  • `clean` could protect you if its got `$mysqli_connection->escape_string($string);` in it. Still, I would use prepared statements. – StackSlave Jul 16 '19 at 23:17
  • @Dharman, thanks for your concern. Well, **clean()** is not an inbuilt php function, it is a function written by me to take care of SQL Injection. – Olajumoke Ademilade Jul 16 '19 at 23:20

1 Answers1

1

Thanks guys, I have been able to figured out what the issue is.

I didn't add [] to module in the html.

Before:

<select name="module" class="browser-default mselect select module">
    <option value="" class="validate" disabled selected>Select User Menu Module</option>  
    <?php echo modules(); ?>
    </select>

after

<select name="module[]" class="browser-default mselect select module">
    <option value="" class="validate" disabled selected>Select User Menu Module</option>  
    <?php echo modules(); ?>
    </select>