Posts: 1,180
Threads: 19
Joined: Jun 2016
Reputation:
55
@richb201,
Suggestion: How about putting in a feature that does a preliminary check to make sure that the specific varchar item is unique prior to allowing you to add the record (maybe an ajax check once the field is filled in.
Posts: 771
Threads: 223
Joined: Sep 2017
Reputation:
0
I am actually building the table with data from another table. So the user adds the field to table a, and in the callback for the insert, I also insert the field into table b. Table b is the one that I want unique. In table a, the field is not unique. Only in table b is it unique.
proof that an old dog can learn new tricks
Posts: 1,404
Threads: 3
Joined: Aug 2017
Reputation:
39
07-21-2020, 07:20 AM
(This post was last modified: 07-21-2020, 07:20 AM by jreklund.)
Either INSERT IGNORE or ON DUPLICATE KEY UPDATE, depending on result you want.
Posts: 771
Threads: 223
Joined: Sep 2017
Reputation:
0
07-22-2020, 06:40 AM
(This post was last modified: 07-22-2020, 07:41 AM by richb201.)
Thanks. I am trying this in a callback:
```
$data = array(
'item' => $post_array['employee_title'],
'campaign' => $this->session->userdata('campaign'),
'userid' => $this->session->userdata('userid')
);
$this->db->insert_ignore ('titles', $data);
```
I am getting this error:
Call to undefined method CI_DB_mysqli_driver::insert_ignore
I don't see insert_ignore in the Query builder documentation, btw.
Any ideas?
proof that an old dog can learn new tricks
Posts: 1,404
Threads: 3
Joined: Aug 2017
Reputation:
39
You need to create that manually.
Posts: 771
Threads: 223
Joined: Sep 2017
Reputation:
0
Thanks. I found this on the mysql website:
INSERT INTO t1 (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
UPDATE t1 SET c=c+1 WHERE a=1;
Can I assume that this is psuedo code to build the sql?
proof that an old dog can learn new tricks
Posts: 771
Threads: 223
Joined: Sep 2017
Reputation:
0
07-22-2020, 11:33 AM
(This post was last modified: 07-23-2020, 06:03 AM by richb201.)
Insite,
still not working, I tried it both with the item field set to unique and not. Is there a log for mysql errors?
$data = array(
'item' => $post_array['cost_center'],
'campaign' => $this->session->userdata('campaign'),
'userid' => $this->session->userdata('userid')
);
$string="INSERT IGNORE INTO exclude_choices(item, campaign, userid) VALUES (" . $this->db->escape($data[item]) . ", " .$this->db->escape($data[campaign]) . ", " .$this->db->escape($data[userid]) . ")";
$this->db->query($string);
You realize that this is being called within a callback and the original table in the controller is "employees" and not "exclude_choices"? I found some mention of moving the code dealing with another table into a function in "model" rather than in the controller. Any commentary on this plan?
proof that an old dog can learn new tricks