Codeigniter : Check duplicate data with num_rows
this workshop use to check a duplicate data with num_rows in codeigniter. eg = colunm ‘mailto’
example code
the table
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE `tbl_mail` ( `mid` int(11) NOT NULL, `mailto` varchar(200) NOT NULL, `mailsub` varchar(200) NOT NULL, `mailmessage` varchar(200) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; ALTER TABLE `tbl_mail2` ADD PRIMARY KEY (`mid`); ALTER TABLE `tbl_mail2` MODIFY `mid` int(11) NOT NULL AUTO_INCREMENT; COMMIT; |
view
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html> <head> <title>form add mail</title> </head> <body> <form action="<?php echo site_url('email/adding');?>" method="post"> <h4>::Form add::</h4> To <br /> <input type="email" name="mailto" required/> <br /> Subject <br /> <input type="text" name="mailsub" required /> <br /> Detail <br /> <textarea name="mailmessage" required></textarea> <br /> <button type="submit">Send</button> </form> </body> </html> |
controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public function adding($value=''){ $mailto = $this->input->post('mailto'); // num rows example $this->db->select('mailto'); $this->db->where('mailto',$mailto); $query = $this->db->get('tbl_mail'); $num = $query->num_rows(); if($num > 0) { echo 'duplicate !!'; }else{ $arr=array( 'mailto'=> $this->input->post('mailto'), 'mailsub'=> $this->input->post('mailsub'), 'mailmessage'=> $this->input->post('mailmessage') ); $this->db->insert('tbl_mail2',$arr); echo 'added !!'; } } |
result
if can add data echo added !! or duplicate data echo duplicate !!.