<?php
//****************************************请求权限****************************************
require("locked.php");                                                                 //*
setlocked(0);//会计科目                                                                 //*
//****************************************************************************************

echo '<script>document.title="处理结果通知";</script>';

include("cnn.php");

//新增前检查ID是否存在上级；
if(strlen($_POST[id])>4){
	$strsql='select * from account where id="'.substr($_POST[id],0,strlen($_POST[id])-2).'"';
    $sql=$mysqli->query($strsql);
	$afr=$sql->num_rows;
	if($afr==0){
		echo '新增失败；<a href="#" onclick="javascript:history.back()">返回</a><br>';
		echo '原因：ID不存在上级；';
		exit();
	}
	$sql->free();
}

//新增数据开始；
$strsql='insert into account(id,name,Fname,t,dc,slwb,xjll,kh,gr,ch,gys,bm,xm,etr)';
$strsql=$strsql.' values("'.floatval($_POST["id"]).'","'.$_POST["name"].'","'.$_POST["name"].'","'.$_POST["t"].'","'.$_POST["dc"].'","'.$_POST["slwb"].'","'.$_POST["xjll"].'",';
$strsql=$strsql.'"'.$_POST["kh"].'","'.$_POST["gr"].'","'.$_POST["ch"].'","'.$_POST["gys"].'","'.$_POST["bm"].'","'.$_POST["xm"].'","'.$_SESSION[username].'")';

$insert=$mysqli->query($strsql);

if($insert){ //新增执行成功；
	echo '新增成功；<a href="#" onclick="javascript:history.back()">返回</a><br>';
	
	//语句开始：该代码自动更新会计科目全称；
	$sqlabc=$mysqli->query('select id,name,Fname from account order by id asc');
	$infoabc=$sqlabc->fetch_array(MYSQLI_BOTH);
	if($infoabc){
		echo '<hr>';
		echo '自动更新会计科目成功！<br>';
		do{
			if(strlen($infoabc[id])!=4){
				//echo substr($infoabc[id],0,strlen($infoabc[id])-2).'<br>';
				$sqlF=$mysqli->query('select Fname from account where id="'.substr($infoabc[id],0,strlen($infoabc[id])-2).'"');
				$infoF=$sqlF->fetch_array(MYSQLI_BOTH);
				$Fname=$infoF[Fname].'-'.$infoabc[name];
				//echo $Fname.'<br>';
				$mysqli->query('update account set Fname="'.$Fname.'" where id="'.$infoabc[id].'"');	
			}
		}while($infoabc=$sqlabc->fetch_array(MYSQLI_BOTH));
	}
	$sqlabc->free();//语句结束；
	   
}else{
	echo '新增失败；<a href="#" onclick="javascript:history.back()">返回</a>';
}
$mysqli->close();
?>