插入mysql时,列不能为NULL

时间:2016-08-08 10:35:16

标签: php mysql codeigniter model-view-controller codeigniter-2

我正在尝试从pr_users中检索与同一个表中的username列匹配的roleid。用户名字段实际上是该特定用户的电子邮件地址。有两个函数,一个是函数add_new_checklist,另一个是函数get_roleid,我将$ dbdata ['责任']从else部分传递给函数get_roleid。我想从pr_users中检索userroleid,它与变量$ dbdata ['责任']中的用户名列相匹配。我从else部分调用函数get_roleid 即$ role_id = $ this-> get_roleid($ var);并附上表格图片。

但是我收到了一个错误:

  

发生数据库错误

     

错误号码:1048

     

列' roleid'不能为空

     

INSERT INTO pr_exit_checklists(action_itemcategory,   responsibilitytarget_completion_periodtarget_completion_day,   target_completion_fromstatusroleid)价值观('新商品',   '管理',' shashankbhat11 @ gmail.com','',' 5',   '辞职日期',' 1',NULL)

我的模型代码是;



			function add_new_checklist($post_array,$action)
{       


						global $USER;
			$post_arr = $this->input->post();
			

			
			global $USER;
			$dbdata = array();
			$fn = 'insert';
					
			$dbdata['action_item'] = $post_array['actions'];
			$dbdata['category'] = $post_array['departmentname'];
		
			if($post_array['responsibility2'] == 'individual')
			{
				$dbdata['responsibility'] = 'Individual';
				
			}
				else if($post_array['responsibility2'] == 'supervisor'){
			$dbdata['responsibility'] = 'supervisor';
			}
			else{
			$dbdata['responsibility'] = $post_array['responsibility'];
				$var = $dbdata['responsibility'];
			$role_id = $this->get_roleid($var);
			}
			$dbdata['target_completion_period'] = $post_array['target_completion_period'];
			$dbdata['target_completion_day'] = $post_array['target_completion_day'];
			$dbdata['target_completion_from'] = $post_array['target_completion_from'];
			$dbdata['status'] = '1';
			$dbdata['roleid'] = $role_id;
			
			/*$dbdata['opening_date'] = date("Y-m-d");*/
			$characters = array(
			"A","B","C","D","E","F","G","H","J","K","L","M",
			"N","P","Q","R","S","T","U","V","W","X","Y","Z",
			"1","2","3","4","5","6","7","8","9");

			//make an "empty container" or array for our keys
			$keys = array();

			//first count of $keys is empty so "1", remaining count is 1-7 = total 8 times
			while(count($keys) < 8) {
				//"0" because we use this to FIND ARRAY KEYS which has a 0 value
				//"-1" because were only concerned of number of keys which is 32 not 33
				//count($characters) = 33
				$x = mt_rand(0, count($characters)-1);
				if(!in_array($x, $keys)) {
				   $keys[] = $x;
				}
			

			foreach($keys as $key){
			   $random_chars .= $characters[$key];
			}
			//$dbdata['ref_no'] =  $random_chars;
			
			$fn = ($action=='inserted')?'insert':'update'; 
			if($fn == 'update' )
			{
				
				$this->db->where("id",$post_array['id']);
				$this->db->$fn('pr_exit_checklists',$dbdata);
				return 'Resignation Request updated successfully';	
			}
			
			if($fn == 'insert' ) { 
				if($this->db->$fn('pr_exit_checklists',$dbdata)){
					$instanceid = ($fn=='insert')?$this->db->insert_id():$post_array['id'];
					
					if($fn=='insert')
					{
							global $USER;
							$fullname = $this->get_name_by_username($USER->username);
							
							
					}
					global $USER;
			$post_arr = $this->input->post();
			
			$manager_id = $this->get_value_by_id('managerid','users',$this->session->userdata('admin_id'));
			$user_id='1';
					$v_memberid = $manager_id . "," . $user_id;
						//$manager_id = $this->get_value_by_id('managerid','users',$this->session->userdata('admin_id'));
					$notification_data['ref_table']			=	'pr_resignation_requests';	
					$notification_data['ref_id']			=	$instanceid;
					$notification_data['modifier_id']		=	$USER->id;
					$notification_data['show_users']		=	$v_memberid;
					$notification_data['notification_descr']=	"Resignation Request Received" ;//$manager_id;
					$notification_data['notification_text']	=	"Resignation Request Received";
					$notification_data['added_on']			=	date("Y-m-d H:i:s");
					$notification_data['url']				=	'exits';
					$notification_data['uurl']				=	'exits';
					$this->db->insert($this->myTables['notifications'],$notification_data);
					return 'Exit Checklist Added Success';
				
				
				$message_body = "Dear Manager,<br><br> A resignation request is being recieved. <br><br>Please find the details below : <br><br>
				Name: ".$rqrow->firstname.' '.$rqrow->lastname."<br><br>
				Email: ".$rqrow->email." <br><br>
				Phone: ".$rqrow->phone_number." <br><br>
				
				
				Please login to the portal <a href='".base_url()."'>".base_url()."</a> to view the complete status of your resource requisitions. <br><br>
				
				<br><br>
				Yours sincerely,<br>
				Admin<br>
			    Mappsit<br>";
				
				$to 	 = $this->get_email_by_id($manager_id);// email id of the recipient
			   
				$subject = "Dear Manager. You have recieved a resignation request";
				$headers = "From: info@mappsit.com\r\n";
				$headers.= "MIME-Version: 1.0\r\n";
				//$headers.= "CC: ".$cc_email_id."\r\n";
				$headers.= "Content-Type: text/html; charset=ISO-8859-1\r\n";
				mail($to, $subject, $message_body, $headers);
				
				
				


					
				}
			}
			return false;
        }

			

				
}


			function get_roleid($var)
			{
				
						
		$sql = "SELECT userroleid FROM `pr_users` WHERE `username` LIKE '%".$var."%'";
		$query=$this->db->query($sql);	
		if($query->num_rows() > 0){
			$result=$query->result();
		}
		return;


}
&#13;
&#13;
&#13;

控制器代码:

&#13;
&#13;
	function add_new_checklist()
		{

	   
	   
	   			global $SITE,$USER;
			$data = array();
			$data['row'] = new stdClass();
			$data['row'] = $this->admin_init_elements->set_post_vals($this->input->post());
			$data['offices']=$this->mod_common->get_all_offices();
			$clients = currentuserclients();
			$data['roles'] = $this->mod_common->get_cat_array('designation','status',"1' AND id > '0",'designation');
			
				$data['reasons'] = $this->exit_common->get_all_reasons();
				$id = $this->uri->segment(3);
			
		

			
			
			//$data['roles'] = $this->exit_common->get_cat_array('designation','status',"1' AND id > '0",'designation');
			get_city_state_country_array($data,array('cityid'=>$data['row']->cityid));
			$data['error_message'] = '';
			$data['row']->id = $this->uri->segment(3);
			$data['id'] = $this->uri->segment(3);			
			$data['action'] = 'add';
			$data['heading'] = 'Add';			
			$data['msg_class'] = 'sukses';
			$data['path']=$path;		
			$post_action = $this->input->post('action');
			if($post_action=='add' || $post_action =='update' ){
				$post_array = $this->input->post();
				$action = ($post_action == 'add')?'inserted':'updated';
				//echo '<pre>';print_r($SITE);die;
				 $post_array['exit_type'] = 'Employee Initiated';
					$data['managerid'] = $this->exit_common->get_managerids($id);
					$data['error_message'] = $this->exit_common->add_new_checklist($post_array,$action);
					
							
					if($data['error_message'] == 'Record '.$action.' successfully'){
						$data['row'] = new stdClass();
						$data['row']->id = $this->uri->segment(3);	
						$data['row']->status = 1;
						
					}
				
			}
			$data['grievance_types'] = $this->mod_common->get_user_allowed_leaves();
			if($data['row']->id>0){
				$data['action'] = 'update';
				$data['heading'] = 'Edit';
				$data['rows'] = $this->mod_common->get_leave_request($data['row']->id);
				$clid = $data['row']->id;	
				$data['row']->id = $clid;
			}
			//$data['my_detail'] = $this->mod_common->get_my_details($USER->id);
			$data['my_detail'] = $this->exit_common->get_details_profile($USER->id,'users_details','userid');	
			$data['my_detail']->userdetail = $this->exit_common->get_details($USER->id,'users');
			get_address($data['my_detail'],ADDRESS_TYPE1,$USER->id);
			$data['cities']=$this->exit_common->get_array_frontend('city');
				$data['category'] = $this->exit_common->get_departments_as_category();
			$data['roles'] = $this->exit_common->get_roles();
				$data['emailid'] = $this->exit_common->get_emailid();
			
			$this->data['maincontent'] = $this->load->view('maincontents/display_form_for_adding_checklist', $data,true); 
			$this->load->view('layout', $this->data);
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
	   
			
		}
&#13;
&#13;
&#13;

查看代码:

&#13;
&#13;
             <div class="col-md-5 pull-right">
                            <div class="control-group form-group">
                                <label class="control-label">
                                   Responsiblity Of
									<span class="req_star">*</span>
                                </label>

                                <div class="controls">
                                   <?php 
												$options = array();
												$options['individual']='Individual';
												$options['supervisor']='Supervisor';
												$options['function']='others';
												echo form_dropdown('responsibility2', $options, explode(",",$row->role),'id="resp_type" class="form-control function col-md-12 select2" reqired="required" ');
											?>
                                </div>
                            </div>
                        </div>
                       
					</div>
						<div style="clear:both"></div>
						<?php if($row->resp_type == 'function') $style="style='display:block'"; else $style="style='display:none;'"; ?>
						<div class="row-fluid rowspace resp_dept" <?php echo $style; ?>>
						 <div class="col-md-5">
                            <div class="control-group form-group">
                    

                                <div class="controls">
									<div class="col-md-9">
             <select name="responsibility" class="form-control">
            <?php 

            foreach($emailid as $row)
            { 
	
              echo '<option value="'.$row->username.'">'.$row->username.'</option>';
            }
            ?>
            </select>
										</div>
                                </div>
                            </div>
                        </div>
						 
&#13;
&#13;
&#13;

pr_users表

enter image description here

pr_exit_checklists表 enter image description here

1 个答案:

答案 0 :(得分:0)

您不允许在pr_exit_checklists表中插入NULL,也不允许从您的查询中插入NULL。 您需要在表中启用allow NULL选项 或者您需要更改插入语句并将roleid传递为0或1。