主键为空

时间:2019-10-01 03:58:31

标签: php mysql sql

我将在这里为以下代码进行解释。我有两个表,分别是tblHrsPositionProgramTemplatetblHrsPositionProgramTemplateLog。 处理流程为:

  1. 检查以查看表tblHrsPositionProgramTemplate中的取消勾选ID(未选中的复选框)。
  2. 如果有取消勾选的ID,请插入 ID到tblHrsPositionProgramTemplateLog中并设置为'n'。
  3. tblHrsPositionProgramTemplate
  4. 删除当前ID(因为避免在视图中重复显示)
  5. 插入,再次在tblHrsPositionProgramTemplatetblHrsPositionProgramTemplateLog中勾选id(已选中的复选框)。

因此,问题是我的tblHrsPositionProgramTemplateLog的ID为空。有人知道我的代码有什么问题吗?我正在尝试多种方法,但结果仍然相同。

Database

  

PHP

function updateTemplate(){
global $ehorsObj;
$employeeID = $_SESSION['employeeID'];
$propertyID = $_SESSION['propertyID'];
$positionTemplateID = (isset($_POST['positionTemplateID']) ? $_POST['positionTemplateID'] : '');
$id = (isset($_POST['id']) ? $_POST['id'] : '');
$progid  = (isset($_POST['progid']) ? $_POST['progid'] : '');
$active  = (isset($_POST['active']) ? $_POST['active'] : '');
$unProgId  = (isset($_POST['unProgId']) ? $_POST['unProgId'] : '');

//solution 1
$sqlReadTable = "SELECT * FROM tblHrsPositionProgramTemplate 
                    WHERE hrsPositionID = '" . $id . "' ";
$GetResult = $ehorsObj->FetchData($sqlReadTable, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
while ($row = $GetResult->fetch()){

    if (in_array($row['programID'], $unProgId)) { //Checking for untick id(unchecked checkbox) in table tblHrsPositionProgramTemplate

        $sqlInsertlog = "INSERT  INTO tblHrsPositionProgramTemplateLog 
                    SET positionTemplateIDLog = '" . $row['positionTemplateIDLog'] . "', 
                    positionTemplateID = '" . $row['positionTemplateID'] . "',
                    programID = '" . $row['programID'] . "',
                    hrsPositionID  = '" . $row['hrsPositionID'] . "',
                    propertyID   = '" . $row['propertyID'] . "',
                    employeeID  = '" .  $row['employeeID']. "',
                    dateTimeEmployee = NOW() ,
                    active = 'n' ";         //If there is untick id, insert id into tblHrsPositionProgramTemplateLog and set as 'n'.

        $ehorsObj->ExecuteData($sqlInsertlog, $ehorsObj->DEFAULT_PDO_CONNECTIONS);

    }
}
//Delete current id in tblHrsPositionProgramTemplate (because to avoid duplicate display in view)
$sqlDelete = "DELETE FROM tblHrsPositionProgramTemplate 
                WHERE hrsPositionID = '".$id."' "; 
$ehorsObj->ExecuteData($sqlDelete, $ehorsObj->DEFAULT_PDO_CONNECTIONS);


for($x=0; $x< sizeof($progid); $x++ )
{
    $positionTemplateID = $ehorsObj->EHORS_PK("tblHrsPositionProgramTemplate"); 
        $sqlAdd = "INSERT  INTO tblHrsPositionProgramTemplate 
                    SET positionTemplateID = '" . $positionTemplateID . "',
                    programID = '" . $progid[$x] . "',
                    hrsPositionID  = '" . $id . "',
                    propertyID   = '" . $propertyID . "',
                    employeeID  = '" . $employeeID . "',
                    dateTimeEmployee = NOW(),
                    active = 'y'";      //Insert again tick id(checked checkbox) in tblHrsPositionProgramTemplate and tblHrsPositionProgramTemplateLog.

        $ehorsObj->ExecuteData($sqlAdd, $ehorsObj->DEFAULT_PDO_CONNECTIONS);

if (in_array($progid[x], $unProgId)){
        //do nothing
    }else{
        $positionTemplateIDLog = $ehorsObj->EHORS_PK("tblHrsPositionProgramTemplateLog");   
        $sqlAddLog = "INSERT  INTO tblHrsPositionProgramTemplateLog 
                    SET positionTemplateIDLog = '" . $positionTemplateIDLog . "',
                    positionTemplateID = '" . $positionTemplateID . "',
                    programID = '" . $progid[$x] . "',
                    hrsPositionID  = '" . $id . "',
                    propertyID   = '" . $propertyID . "',
                    employeeID  = '" . $employeeID . "',
                    dateTimeEmployee = NOW(),
                    active = 'y'";      //masuk dlm log id yg tick

        $ehorsObj->ExecuteData($sqlAddLog, $ehorsObj->DEFAULT_PDO_CONNECTIONS);
    }
}}  

0 个答案:

没有答案