CodeIgniter Forums
Saving data to MYSQL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Saving data to MYSQL (/showthread.php?tid=74878)



Saving data to MYSQL - dmorgan20 - 11-19-2019

I have followed an online guide how to do this. But its not working. I've made some slight changes but nothing is working, help

User inputs info page:


Code:
<fieldset class="col-xs-12 col-sm-6 personal-info-wrapper">
                        <input type="hidden" id="user-id">
                        <form method="post">
                            <div class="form-group">
                                <label for="postcode">Enter Postcode Area</label>
                                <input style="font-size:12px" name="postCode" id="postcode" class="form-control required" value="e.g CF11">
                            </div>
                             <div class="form-group">
                                <label for="deliveryCost">Delivery Charge</label>
                                <input style="font-size:12px" name="delivery" id="deliveryCost" class="form-control required" value="e.g 5.00">
                            </div>
                            <button type="button" class="save-settings btn btn-primary btn-xs"
                                title="<?= lang('save') ?>">
                                <span class="glyphicon glyphicon-floppy-disk"></span>
                                <?= lang('save') ?>
                            </button>
                            <input type="submit" name="savePostcode" value="Save Data"/>
                        </form>

</fieldset>


Controller
Code:
    /*load database libray manually
    $this->load->database();       */

    /*load Model
    $this->load->model('Settings_model');   */


    }

    public function savedata()
    {
        /*load registration view form*/
        $this->load->view('insert');

        /*Check submit button */
        if($this->input->post('savePostcode'))
        {

        $first_name=$this->input->post('postCode');
        $last_name=$this->input->post('delivery');

        $this->Settings_model->saverecords($postCode,$delivery);
        echo "Records Saved Successfully";
        }
    }

Model
Code:
    /* Insert postcodes to table*/

    function saverecords($postCode,$delivery)
    {

    $query="insert into Postcodes values('','$postCode','$delivery')";
    $this->db->query($query);
    }



RE: Saving data to MYSQL - InsiteFX - 11-19-2019

you need to save the record using an associated array.

PHP Code:
// Controller
public function savedata()
{
    /*load registration view form*/
    $this->load->view('insert');

    /*Check submit button */
    if($this->input->post('savePostcode'))
    {
    
$data = [
            'first_name' => $this->input->post('postCode'),
            'last_name'  => $this->input->post('delivery'),
        ];

        $this->Settings_model->saverecords($data);
        echo "Records Saved Successfully";
    }
}

// Model

/* Insert postcodes to table*/
public function saverecords($data)
{
    return $this-db->insert('Postcodes'$data);


SEECodeIgniter User Guide - Database Reference -> Query Builder Class