简体   繁体   中英

PostgreSQL Update Issue With Quotation('')

I'm using PostgreSQL & Codeigniter. There is a table called folio in the database. It has few columns containing remarks1, remarks2, remarks3 as well. Data for the all the other columns are inserted when the INSERT statement executes for the 1st time.

When I try to execute below UPDATE statement later for the below 3 columns, remarks1 column get updated correctly. But remarks2, remarks3 columns are updated with '' .

UPDATE "folio" SET "remarks1" = 'test remark', "remarks2" = '', "remarks3" = '' WHERE "id" = '51';

Given that remarks1, remarks2, remarks3 columns data type is character varying . I'm using Codeigniter active records. At a time all 3 columns could be updated else single column could be updated depending on the user input.

What could be the issue? How can I fix this? Why columns are updated with '' ?

As requested the php array in CI would be below

$data     = array(
     'remark1' => $this->input->post('remark1'),
     'remark2' => $this->input->post('remark1'),
     'remark3' => $this->input->post('remark1')
);

Function which saves the data contains below two lines only

$this->db->where('id', $folio_id);
$this->db->update('folio', $data);

Those columns are updated with '' because you tell them to?
Let's take a closer look at the query

UPDATE "folio"
SET
    "remarks1" = 'test remark',
    "remarks2" = '',
    "remarks3" = ''
WHERE
    "id" = '51';

First you select the table folio for the update.
Then you tell it to update remarks1 through remarks3 with new values. For remarks2 and remarks3 you specify to set them to an empty string. And that's what's going to happen.
Last but not least, you tell it to only apply this update to rows where id equals 51 .

So, in order to only update remarks1 you can simply remove the other columns from your update:

UPDATE "folio"
SET
    "remarks1" = 'test remark'
WHERE
    "id" = '51';

Update:
I'm by far not a CI expert, but from what I see, I'd change the $data array to only contain information for remark1 :

$data     = array(
     'remark1' => $this->input->post('remark1')
);

And (from my understanding) it should only update this single column.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM