简体   繁体   中英

Using CodeIgniter, how to find out if a record already exists in a database?

I have to check whether the email id and phone not already updated in the database or not.

Can you please help me to find solution for this?

Controller code:

public function add_lead()
    {       
        if($this->session->userdata('logged_in') && ($this->session->userdata('role')=='support'))
        {
            date_default_timezone_set('Asia/Calcutta');
            $id='';

            $id= $this->uri->segment(4);
            $data = array(
            'title' =>"add_lead",
             'name' => $this->input->post('name'),
             'company' => $this->input->post('company'),
             'company_type' => $this->input->post('company_type'),
             'email1' => $this->input->post('email1'),
             'email2' => $this->input->post('email2'),               
             'phone1' => $this->input->post('phone1'),
             'phone2' => $this->input->post('phone2'),
             'website_url' => $this->input->post('website_url'),
             'fb_url' => $this->input->post('fb_url'),
             'city' => $this->input->post('city'),
             'other_info' => $this->input->post('other_info'),             
              'status'=>'Active'
                                ); 

                $rul =  array(  
                array('field' => 'name', 'label' => 'Name', 'rules' => 'required'),
                //array('field' => 'company_logo', 'label' => 'Company Logo', 'rules' => 'required')
                );  

                 $email1=$this->input->get("email1");

                $this->form_validation->set_rules($rul);
                $this->form_validation->set_rules('email1', 'email1', 'callback_rolekey_exists'); 

                if ($this->form_validation->run() == FALSE)
                {
                    $this->load->view($this->support_header,$data);
                    $this->load->view($this->add_lead,$data);
                    $this->load->view($this->support_footer);
                }

                else
                { 
                $unique=getUniqueID(12);    

                $unique_id= getUniqueID(6);

            if(!isset($error))
                    {
                        if(isset($_POST['submit']))
                            {
                    $ser=mysql_query("SELECT COUNT(*) as count from support_leads where email1='".$email1."' ");
                    $count=mysql_fetch_assoc($ser); 
                    $count=$count['count'];
                    if($count==1)
                    {
                    echo 'email already existing';
                    }
                            }
                            else{
                    $fields = array(

                     'name' => $this->input->post('name'),
                     'company' => $this->input->post('company'),
                     'company_type' => $this->input->post('company_type'),
                     'email1' => $this->input->post('email1'),
                     'email2' => $this->input->post('email2'),               
                     'phone1' => $this->input->post('phone1'),
                     'phone2' => $this->input->post('phone2'),
                     'website_url' => $this->input->post('website_url'),
                     'fb_url' => $this->input->post('fb_url'),
                     'city' => $this->input->post('city'),
                     'others' => $this->input->post('other_info'),
                     'created_date' => date("Y-m-d H:i:s"),
                     'lead_status' => 'New',
                      'status'=>'Active'
                             );

                        $this->am->insertdata("support_leads",$fields);
                        redirect('support/leads/');
                    }
                }
                    else
                    {
                        $this->load->view($this->support_header,$data);
                        $this->load->view($this->add_lead,$data);
                        $this->load->view($this->support_footer);   
                    }


                }
        }
        else 
        {
            $this->login();
        }
    }

View Code:

                <div>
                <ul class="breadcrumb">
                    <li>
                        <a href="<?php echo site_url('support/index/');?>">Dashboard</a> <span class="divider">/</span>
                    </li>
                    <li>
    <a href="<?php echo site_url('support/add_lead/');?>" >Add Lead</a>
                    </li>
                </ul>
            </div>

            <div class="row-fluid sortable">
                <div class="box span12">
                    <div class="box-header well" data-original-title>
                        <h2><i class="icon-edit"></i> <?php echo $title;?></h2>

                    </div>

                    <div class="box-content" style="color:#F00">
                                              <?php if(!empty($msg)) 
                                                 {
                                               echo $msg; 
                                                  }
                                            ?>
                                         </div>


                    <div class="box-content">
                        <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
                          <fieldset>
                          <div class="control-group">
                                <label class="control-label" for="inputSuccess">Name</label>
                                <div class="controls">
                                  <input type="text" id="name" name="name" value="<?php echo $name; ?>" required/>
          <?php if(form_error('name')!='') { ?>
            <div class="error-left"></div>
            <div class="error-inner"><?php echo form_error('name'); ?></div><?php } ?>
                                </div>
                              </div>
                              <div class="control-group">
                                <label class="control-label" for="inputSuccess">Company</label>
                                <div class="controls">
                                  <input type="text" id="company" name="company" value="<?php echo $company; ?>" />
          <?php if(form_error('company')!='') { ?>
            <div class="error-left"></div>
            <div class="error-inner"><?php echo form_error('company'); ?></div><?php } ?>
                                </div>
                              </div>
                              <div class="control-group">
                                <label class="control-label" for="inputSuccess"> Company Type </label>
                                <div class="controls">
                                <select name="company_type" class="" style="" >              
                                 <option value="">Select Type</option>
                                 <option value="E-commerce" <?php if($company_type == 'E-commerce') echo "selected";?> > E-commerce </option>                    
                                 <option value="SME" <?php if($company_type == 'SME') echo "selected";?> > SME </option>
                                 <option value="Enterprise" <?php if($company_type == 'Enterprise') echo "selected";?> > Enterprise </option>
                                 <option value="Individual" <?php if($company_type == 'Individual') echo "selected";?> > Individual </option>                                


                                 </select>


                                </div>
                              </div>


                          <div class="control-group">
                                <label class="control-label" for="inputSuccess">Email 1</label>
                                <div class="controls">
                                  <input type="text" id="email1"  name="email1" value="<?php echo $email1; ?>" />

                      <?php if(form_error('email1')!='') { ?>
            <div class="error-left"></div>
            <div class="error-inner"><?php echo form_error('email1'); ?></div><?php } ?>
                                </div>
                              </div>
                              <div class="control-group">
                                <label class="control-label" for="inputSuccess">Email 2</label>
                                <div class="controls">
                                  <input type="text" id="email2"  name="email2" value="<?php echo $email2; ?>" />
          <?php if(form_error('email2')!='') { ?>
            <div class="error-left"></div>
            <div class="error-inner"><?php echo form_error('email2'); ?></div><?php } ?>
                                </div>
                              </div>                    

                          <div class="control-group">
                                <label class="control-label" for="inputSuccess">Phone 1</label>
                                <div class="controls">
                                  <input type="text" id="phone1"  name="phone1" value="<?php echo $phone1; ?>" />
          <?php if(form_error('phone1')!='') { ?>
            <div class="error-left"></div>
            <div class="error-inner"><?php echo form_error('phone1'); ?></div><?php } ?>
                                </div>
                              </div>
                              <div class="control-group">
                                <label class="control-label" for="inputSuccess">Phone 2</label>
                                <div class="controls">
                                  <input type="text" id="phone2"  name="phone2" value="<?php echo $phone2; ?>" />
          <?php if(form_error('phone2')!='') { ?>
            <div class="error-left"></div>
            <div class="error-inner"><?php echo form_error('phone2'); ?></div><?php } ?>
                                </div>
                              </div>

                              <div class="control-group">
                                <label class="control-label" for="inputSuccess">website URL </label>
                                <div class="controls">
                                  <input type="text" id="website_url"  name="website_url" value="<?php echo $website_url; ?>" />

                                </div>
                              </div>
                              <div class="control-group">
                                <label class="control-label" for="inputSuccess">Facebook URL </label>
                                <div class="controls">
                                  <input type="text" id="fb_url"  name="fb_url" value="<?php echo $fb_url; ?>" />

                                </div>
                              </div>
                              <div class="control-group">
                                <label class="control-label" for="inputSuccess">City </label>
                                <div class="controls">
                                  <input type="text" id="city"  name="city" value="<?php echo $city; ?>" />

                                </div>
                              </div>


                          <div class="control-group">
            <label class="control-label" for="inputSuccess">Other Info</label>
            <div class="controls">

                <textarea  name="other_info"  style="height:130px;"  ></textarea>

            </div>
            </div>


                            <div class="form-actions">
                <?php if(isset($_REQUEST['id']))

                {?>
<input type="hidden" value="<?php echo $_REQUEST['id']; ?>" name="id"  />   
<input type="submit" value="Update Changes" name="submit" class="btn btn-primary" />
                <?php }
                else {?>
                    <input type="submit" id="submit" value="Add Changes" name="submit" class="btn btn-primary" />
                <?php }?>
                 <!--   <input type="hidden" value="<?php echo $user_id; ?>" name="user_id" />-->

                    <input type="reset" value="Cancel"  class="btn btn-primary" />

                            </div>

                          </fieldset>
                        </form>   

                    </div>
                </div><!--/span-->

            </div><!--/row-->

As simple as

$this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[TABLE NAME.FIELD NAME]');

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