预排序遍历树,当子节点插入数据失败时,需要进行回滚。


M层:

//新加
		function add_projKind($gongchengjianming,$level,$qishijuli,$wdate,$beizhu){
				//$query = $this->db->query("select * from student");				
				//$return['boolean'] = FALSE;
				
				$this->db->query("set autocommit=0");
				$this->db->query("begin;");

				$query = $this->db->query("select * from constrproj");
				$result_count = count($query->result());
				$result_count2 = 0;

				if($result_count==0){
					//$return['boolean'] = TRUE;
					$this->db->query("insert constrproj (ConstrProjName,CPKind,Mileage,ProjStart,ProjNote,PLeft,PRight,KindLvl) values('$gongchengjianming','$level','$qishijuli','$wdate','$beizhu',1,2,0) " );
					$this->db->query("insert constrprojact (ConstrProjACTName,CPKind,Mileage,ProjStart,ProjNote,PLeft,PRight,KindLvl) values('$gongchengjianming','$level','$qishijuli','$wdate','$beizhu',1,2,0) " );
					
					
					$query2 = $this->db->query("select * from constrproj");
				    $result_count2 = count($query2->result());
					
				}else{

					$this->db->query("select @myLeft:=PLeft from constrproj where  ConstrProjID = (select max(ConstrProjID) from constrproj)");					
					$this->db->query("UPDATE constrproj SET PRight = PRight + 2 WHERE PRight > @myLeft");					
					$this->db->query("UPDATE constrproj SET PLeft = PLeft + 2 WHERE PLeft > @myLeft");					
					$this->db->query("INSERT INTO constrproj(ConstrProjName, PLeft, PRight) VALUES('$gongchengjianming', @myLeft + 1, @myLeft +2)" );
					
					$query2 = $this->db->query("select * from constrproj");
				    $result_count2 = count($query2->result());			
				   

				}

				if($result_count<>$result_count2){
					$message = "创建建名工程成功!";
					$this->db->query("set autocommit=1");
					$return['boolean'] = TRUE;
					$return['message'] = $message;
					return $return;
				}else{
					$message = "创建建名工程失败!";
					$this->db->query("rollback"); 
					$this->db->query("set autocommit=1");
					$return['boolean'] = FALSE;
					$return['message'] = $message;
					return $return;
				}

				
			}

C层及显示

前台显示

function add_jianming(){
			$gongchengjianming =trim($this->input->post('gongchengjianming'));	
			$level =trim($this->input->post('level'));
			$qishijuli =trim($this->input->post('qishijuli'));			
			$wdate =trim($this->input->post('wdate'));
			$beizhu =trim($this->input->post('beizhu'));
			

			//echo $beizhu;

			//die();


			$this->load->model('project_m');
			$return = $this->project_m->add_projKind($gongchengjianming,$level,$qishijuli,$wdate,$beizhu);
			
			<span style="background-color: rgb(255, 102, 102);">if($return['boolean']){
				echo '<script>alert("'.$return['message'].'");location.href="'.base_url().'project/projectName";</script>';
			}else{
				echo '<script>alert("'.$return['message'].'");location.href="'.base_url().'project/projectName";</script>';
			}</span>
		
		}




本文转载:CSDN博客