与codeigniter相关的SQL查询

时间:2016-07-07 14:44:25

标签: php sql codeigniter

查看:::

                       <form method="post" action="<?php echo base_url(); ?>Signup/profile">


                        <div class="form-group">
                            <label>Company Name</label>
                            <input type="text" class="form-control" name="company_name" value="<?php echo  $det->company_name; ?>" required>
                        </div>
                         <div class="form-group">
                            <label>Upload Company Logo</label>
                            <input type="file" name="company_pic" accept="image/*" class="form-control" vlaue="<?php echo $det->company_pic; ?>" required>
                        </div>
                        <div class="form-group">
                            <label>Upload Profile Picture</label>
               				<input type="file" name="profile_pic" accept="image/*" class="form-control" vlaue="<?php echo $det->profile_pic; ?>" required>
                        </div>

                        <div class="form-group">
                            <label>Projects Done</label>
                            <input type="number" class="form-control" name="projects_done" value="<?php echo $det->projects_done; ?>" required>
                        </div>

                        <div class="form-group">
                            <label>No Of Employees</label>
                            <input type="number" class="form-control" name="num_of_emp" value="<?php echo $det->num_of_emp; ?>"required>
                        </div>

                        <div class="form-group">
                            <label>Founding Year</label>
							</br>
							</br>
                            <input type="number" class="form-control" name="found_year" value="<?php echo $det->found_year; ?>" required>
                        </div>

                        <div class="container">
                            <label>Service Offering</label></br>
							 <?php
$new_array=array('1'=>'Engineering and Design','2'=>'Operations and support','3'=>'Product management','4'=>'Developer relations and technical solutions','5'=>'Sales and account management','6'=>'Partnerships','7'=>'Sales and operations','8'=>'Administrative services','9'=>'Business strategy planning','10'=>'Finance solutions','11'=>'Legal and government relations','12'=>'Marketing and communications','13'=>'Real estate and workplace services','14'=>'Social impact solutions','15'=>'Consultancy services','16'=>'Investors and funding');

  if(isset($new_array)) 
	  $services = array_column($services,'service_offered');
         foreach($new_array as $key=>$val):

        if($db_key = array_search($val,$services))
         {  ?>
            <input type="checkbox" name='service[]'    value="<?php echo $key ?>" checked ><?php echo $val; ?> <br/>
       <?php  }
         else
         { 
?>
           <input type="checkbox" name='service[]'    value="<?php echo $key ?>" ><?php echo $val; ?><br/>
        <?php   }

 endforeach; ?>

							
                            
                        </div>
<br><br>
                        <div class="form-group">
                            <label>Locations</label>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name='locations' value="1" <?php if($det->locations==="Hyderabad")echo "checked";?>>Hyderabad
                                </label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name='locations' value="2" <?php if($det->locations==="Pune")echo "checked";?>>Pune
                                </label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name='locations' value="3" <?php if($det->locations==="Bangalore")echo "checked";?>>Banglore
                                </label>
                            </div>
                            <div class="checkbox">
                                <label>
                                    <input type="checkbox" name='locations' value="4" <?php if($det->locations==="Delhi")echo "checked";?>>Delhi
                                </label>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name='locations' value="5" <?php if($det->locations==="Kolkata")echo "checked";?>>Kolkata
                                    </label>
                                </div>
                                <div class="checkbox">
                                    <label>
                                        <input type="checkbox" name='locations' value="6" <?php if($det->locations==="Chennai")echo "checked";?>>Chennai
                                    </label>
                                </div>
                            </div>
                          <button type="submit" value="Update" class="btn btn-primary">Update</button>
                           </form>

控制器::

public function profile()
     {
		$vendor=$this->input->post();
		
		$service_id=$this->input->post('service');
		unset($vendor['service']);
		
	    $loc_id=$this->input->post('locations');
	    $login_email=$this->session->userdata('email_id');
		$vendor['locations']=$this->Vendormodel->get_location($loc_id);
		print_r($vendor); exit;
	    $this->Vendormodel->add_vendor_profile($login_email,$vendor);
		$id1=$this->Vendormodel->get_vendor_id($login_email);
		 if(isset($service_id))
		foreach($service_id as $id){
		$post[]=$this->Vendormodel->get_services($id);
		}
		if(isset($service_id))
		foreach($post as $service){
		$this->Vendormodel->add_vendor_services($id1,$service);
		}
		
		$vendordash['det']=$this->Vendormodel->vendor_details($login_email);
		$vendordash['services']=$this->Vendormodel->vendor_services($id1);
		
		$this->load->view('vendor/vendor_dashboard',$vendordash);
	
	 }

模型::

public function add_vendor_services($id,$service)
		{
			$query = $this->db->get('vendor_services');

            foreach ($query->result() as $row)
            {
             $ids[]=$row->id;
			 
            }
			if(isset($ids)){
           foreach($ids as $vid){
			if($id==$vid){
				$q1 =$this->db->get_where('vendor_services',array('id'=>$vid));
				$serv=$q1->row()->service_offered;
				if($serv===$service)
					return;
				else{
		$this->db->set('id',$id)
		         ->set('service_offered',$service)
		         ->insert('vendor_services');
			return;
			}}else{$this->db->set('id',$id)
		         ->set('service_offered',$service)
		         ->insert('vendor_services');
				 return;
				 }
			}}
		else
			{$this->db->set('id',$id)
		         ->set('service_offered',$service)
		         ->insert('vendor_services');
				 return;
				 }
			
		
		}
		

我在数据库中有一个表,表名为:vendor_services。它包含'id'和'service_offered'。我试图从服务在数组中的表单获取供应商ID和服务,并将其放在vendor_services表中。但我不想为已经存在的特定供应商ID插入服务。

假设如果service1再次出现在供应商ID 1中,那么供应商1的service1已经存在于表中,不应该插入它。

1 个答案:

答案 0 :(得分:0)

如果vendor_services(id,service_offered)上没有唯一键,那么您需要先自己手动检查组合,然后决定插入与否。这应该适用于这种情况:

public function add_vendor_services($id,$service) {
            $this->db->select('*')->from('vendor_services')
                ->where('id',$id)
                ->where('service_offered',$service);

            if($this->db->get()->num_rows() > 0){
                // already have this service for this vendor
            } else {
                // need to add this service to this vendor
                $this->db->insert('vendor_services',array('id'=>$id,'service_offered'=>$service));
            }
}

如果id&amp;上有唯一的密钥service_offered列然后你可以这样做:

public function add_vendor_services($id,$service) {
    $this->db->replace('vendor_services',array('id'=>$id,'service_offered'=>$service));
}