Search code examples
phpcodeigniterexpressionengine

code igniter / expression engine - insert multiple rows into custom database table


I have an expression engine CMS which lists out a series of members and allows the user to vote on each one and add comments. I then need to submit all these members as separate rows into a custom database table I created in my EE installation. I'm not sure how exactly to call the form that does the submitting from EE though. Here's my code:

FORM

<tr>
    <td><input type="text" name="user[0][name]" value=""></td>
    <td><input type="text" name="user[0][party]" value=""><br></td>
    <td><input type="text" name="user[0][marks]" value=""></td>
    <td><input type="text" name="user[0][comments]" value=""></td>
</tr>
<tr>
    <td><input type="text" name="user[1][name]" value=""></td>
    <td><input type="text" name="user[1][party]" value=""><br></td>
    <td><input type="text" name="user[1][marks]" value=""></td>
    <td><input type="text" name="user[1][comments]" value=""></td>
</tr>

SUBMISSION CODE:

foreach($_POST['user'] as $user)
{
    $this->db->insert('mytable', $user);
}

But how do I call this php code from the EE form? Is it just a simple <form action="url.php">? And how would I set up that php code to utilize EE's database connection strings?


Solution

  • Aditya isn't far off, you just need to instantiate EE. So for your form targets a result page/template as normal html, for example: <form action="/form-result"> (or whatever), then the quick and dirty way would be simply to include PHP in the template receiving the form.

    On that page (receiving the form), have the following (template must have PHP enabled):

    <?php
    $EE =& get_instance();
    $postUsers = $EE->input->post('user');
    
    foreach($postUsers as $user) {
        // Extra Cleaning
        $user = $EE->db->escape_str($user);
        $user = $EE->security->xss_clean($user);
        // Fields and values to save to DB
        $dbFields = array(
            'user' => $user['name'],
            'party' => $user['party'],
            'marks' => $user['marks'],
            'comments' => $user['comments']
        );
        // Prefix variable used for good practice, but could hard code
        $EE->db->insert($EE->db->dbprefix . 'mytable', $dbFields);
    }
    ?>
    

    I've shown the long method of recreating the array to send to the DB in case you want to modify or add more fields, but you could simply just pass $user (after cleaning) to the database as the array will have the same structure.

    If it's multiple POST variables sent by the form (instead of the array you imply), you'd need something like:

    foreach($_POST as $var) {
        $postUsers = $EE->input->post($var);
    

    A more ideal solution would be developing a plugin that would be called from the resulting template to trigger the above, or an extension that 'listened' for the appropriate form to be submitted, possibly by using the ACT security ability built into EE. But that's probably added an unnecessary level of complexity for your requirement.