php mysql input saves only zero to db Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of php mysql input saves only zero to db without wasting too much if your time.

The question is published on by Tutorial Guruji team.

<div class="control-group invoice-properties">
    <label class="control-label"><?php echo lang('quote_type'); ?></label>
    <div class="controls">
        <input type="text" id="quote_type" class="input-small" value="<?php echo $quote->quote_type; ?>" style="margin: 0px;">    
    </div>
</div>          

The above code is located in a form. When I load the form it retrieves the value from the database. So when I enter directly in the database a number it retrieves this. But when I save it always saves a zero. When I input 8 it makes a 0(zero).

Anyone any idea why this is. Thanks in advance.

Below is the code that does the saving/retrieving at least I think it does.

public function save() {
    $this->load->model('quotes/mdl_quote_items');
    $this->load->model('quotes/mdl_quotes');
    $this->load->model('item_lookups/mdl_item_lookups');

    $quote_id = $this->input->post('quote_id');

    $this->mdl_quotes->set_id($quote_id);

    if ($this->mdl_quotes->run_validation('validation_rules_save_quote')) {
        $items = json_decode($this->input->post('items'));

        foreach ($items as $item) {
            if ($item->item_name) {
                $item->item_quantity = standardize_amount($item->item_quantity);
                $item->item_price    = standardize_amount($item->item_price);

                $item_id = ($item->item_id) ? : NULL;

                $save_item_as_lookup = (isset($item->save_item_as_lookup)) ? $item->save_item_as_lookup : 0;

                unset($item->item_id, $item->save_item_as_lookup);

                $this->mdl_quote_items->save($quote_id, $item_id, $item);

                if ($save_item_as_lookup) {
                    $db_array = array(
                        'item_name'        => $item->item_name,
                        'item_description' => $item->item_description,
                        'item_price'       => $item->item_price
                    );

                    $this->mdl_item_lookups->save(NULL, $db_array);
                }
            }
        }

        $db_array = array(
                'quote_number'       => $this->input->post('quote_number'),
                'quote_type'    => $this->input->post('quote_type'),
                'quote_date_created' => date_to_mysql($this->input->post('quote_date_created')),
                'quote_date_expires' => date_to_mysql($this->input->post('quote_date_expires')),
                'quote_status_id'    => $this->input->post('quote_status_id')
        );

        $this->mdl_quotes->save($quote_id, $db_array);

        $response = array(
                'success' => 1
        );
    } else {
        $this->load->helper('json_error');
        $response = array(
            'success'           => 0,
            'validation_errors' => json_errors()
        );
    }

    if ($this->input->post('custom')) {
        $db_array = array();

        foreach ($this->input->post('custom') as $custom) {
            // I hate myself for this...
            $db_array[str_replace(']', '', str_replace('custom[', '', $custom['name']))] = $custom['value'];
        }

        $this->load->model('custom_fields/mdl_quote_custom');
        $this->mdl_quote_custom->save_custom($quote_id, $db_array);
    }

    echo json_encode($response);
}

Edit 1

The last comments and answers pointed out the name field is not visible. I have uploaded all files that correspond with this to http://websiterheine.eu/quotes.zip

When you look at views/view.php then there are no name fields on any input. But all inputs are correctly working except the one I asked the question about.

Please take a look at the .zip.

Edit 2

As pointed out by Patrick a .zip ain’t save. So no one will download. Therefore I will post the files on JSFIDDLE. The fiddles wont work because I use it only as a placeholder for the files.

**views/view.php**

http://jsfiddle.net/ty9f7y9u/

**models/mdl_quotes.php**

http://jsfiddle.net/4vngkegm/1/

**controllers/ajax.php**

http://jsfiddle.net/forv452m/1/

**controllers/quotes.php**

http://jsfiddle.net/1gjc3q6n/

Edit 3

I just checked again. There is nowhere in any files $_POST. So it is very strange it is saving all form values.

I have never came across a form like this with no name attributes and no $_POST values. How is this form saving and loading and where is that exactly???

Answer

I found it with help from someone else wtf8_decode thank you for helping. Also eveyone else thank you.

So the solution was to add in views/view.php the line

quote_type: $('#quote_type').val(),

to

$.post("<?php echo site_url('quotes/ajax/save'); ?>", {
        quote_id: <?php echo $quote_id; ?>,
        quote_number: $('#quote_number').val(),
        quote_date_created: $('#quote_date_created').val(),
        quote_date_expires: $('#quote_date_expires').val(),
        quote_status_id: $('#quote_status_id').val(),
        items: JSON.stringify(items),
        custom: $('input[name^=custom]').serializeArray()
    },

So it would look like this:

$.post("<?php echo site_url('quotes/ajax/save'); ?>", {
        quote_id: <?php echo $quote_id; ?>,
        quote_number: $('#quote_number').val(),
        quote_type: $('#quote_type').val(),
        quote_date_created: $('#quote_date_created').val(),
        quote_date_expires: $('#quote_date_expires').val(),
        quote_status_id: $('#quote_status_id').val(),
        items: JSON.stringify(items),
        custom: $('input[name^=custom]').serializeArray()
    },

Now it is working as it should!

We are here to answer your question about php mysql input saves only zero to db - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji