PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] How to check values are inserted or not in Codeigniter

 January 23, 2022     codeigniter, mysql     No comments   

Issue

I have values 3 seperated by comma like (AC,BC,MC).Some times the valuse will be AC only or AC,BC or AC,MC etc

I want to check it according to f_id from table like below

id    f_id    value

1      1       AC

2      1       BC

3      1       MC

4      2       AC

5      3       MC

If any value is not in the table then I need insert it according to f_id

How to do this?


Solution

basically, you want to explode the user input string and get all the results for the current user and compare them. if a value exists in the db that exists in the user input then we don't do anything, if it doesn't then we perform an insert.

$input = 'AC,BC';
$f_id = 1;

$items = explode(',', $input);

$res = $this->db->get_where('yourtablename', array('f_id', $f_id))->result_array();

$values = array_column($res, 'value');

foreach ($items as $index => $value) {

   $value = trim($value); // remove whitespace; you might want to do more filtering/data manipulation than this

   if (!in_array($value, $values)) {
       $data = array('f_id' => $f_id, 'value' => $value);
       $this->db->insert('yourtablename', $data);
   }

}


Answered By - Alex
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing