lastInsertId()导致程序崩溃

时间:2015-02-23 19:22:13

标签: php mysql

所以我在下面有这个功能。在最底层,我正在返回$this->conn->lastInsertId()。如果我没有弄错,这应该是正确的。

文件#1

 public function insertData($table, $qty, $data){                                                                
    $errors = "";                                                                                            
    $columns = $this->fetchColumnNames($table);                                                              
    $column_list = implode(', ', $columns);                                                                  
    $values = array_fill(0, $qty, '?');                                                                           
    $value_list = implode(', ', $values);                                                                         
    $q = "INSERT INTO $table ($column_list) VALUES ($value_list)";                                                
    $stmt = $this->conn->prepare($q);                                                                             
    for($i = 1; $i < $qty + 1; $i++){                                                                             
        foreach($data as $key => $val){                                                                           
            if($key == $columns[$i-1]){                                                                           
                try{                                                                                              
                    if(is_numeric($val)){                                                                         
                        $stmt->bindValue($i, $val, PDO::PARAM_INT);                                              
                    }else{                                                                                        
                        $stmt->bindValue($i, $val, PDO::PARAM_STR);                                               
                    }                                                                                             
                }catch(PDOException $e){                                                                          
                    print  $e->getMessage();                                                                      
                }                                                                                                 
            }                                                                                                     
        }                                                                                                         
    }                                                                                                             
    try{                                                                                                          
        $stmt->execute();                                                                                         
    }catch(PDOException $e){                                                                                      
        $errors .=  $e->getMessage();                                                                             
        print $errors;                                                                                            
    }                                                                                                             
    return $this->conn->lastInsertId();
}

所以继续我的主文件,我有以下代码行

文件#2

error_reporting(E_ALL);
ini_set('display_errors', 1);
include '../../inc/config.php';
include 'ProcessApplication/ProcessApplication.php';
include 'ProcessApplication/ProcessApplicationQuery.php';
$processor = new ProcessApplicationQuery($CONN);
$processor->insertData("full_application", 17, $general);
if($processor->getConn()){
    list($general, $phones) = $processor->processGeneralInfo($_REQUEST['general_information']);
    print $processor->insertData("full_application", 17, $general);
}

如果我从文件#1中删除返回行,只是在文件#2中打印没有任何反应,程序就像这样插入数据库。由于某种原因,这个返回行导致整个程序失败并且404.我很奇怪我从未发生这种情况,为什么这个返回行会导致这个错误?

=== UPDATE ===

所以即使更奇怪,我已将以下项目添加到我的文件中 文件#1

 public function insertData($table, $qty, $data){                                                                
    $errors = "";                                                                                            
    $columns = $this->fetchColumnNames($table);                                                              
    $column_list = implode(', ', $columns);                                                                  
    $values = array_fill(0, $qty, '?');                                                                           
    $value_list = implode(', ', $values);                                                                         
    $q = "INSERT INTO $table ($column_list) VALUES ($value_list)";                                                
    $stmt = $this->conn->prepare($q);                                                                             
    for($i = 1; $i < $qty + 1; $i++){                                                                             
        foreach($data as $key => $val){                                                                           
            if($key == $columns[$i-1]){                                                                           
                try{                                                                                              
                    if(is_numeric($val)){                                                                         
                        $stmt->bindValue($i, $val, PDO::PARAM_INT);                                              
                    }else{                                                                                        
                        $stmt->bindValue($i, $val, PDO::PARAM_STR);                                               
                    }                                                                                             
                }catch(PDOException $e){                                                                          
                    print  $e->getMessage();                                                                      
                }                                                                                                 
            }                                                                                                     
        }                                                                                                         
    }                                                                                                             
    try{                                                                                                          
        $stmt->execute();                                                                                         
    }catch(PDOException $e){                                                                                      
        $errors .=  $e->getMessage();                                                                             
        print $errors;                                                                                            
    }                                                                                                             

}

public function getApplicationid(){
    $query = "SELECT id FROM eagle.full_application ORDER BY create_date DESC LIMIT 1";
    $id = null;
    foreach($this->conn->query($query) as $row){
        $id = $row['id'];
    }
    return $id;
}

文件#2

error_reporting(E_ALL);
ini_set('display_errors', 1);
include '../../inc/config.php';
include 'ProcessApplication/ProcessApplication.php';
include 'ProcessApplication/ProcessApplicationQuery.php';
$processor = new ProcessApplicationQuery($CONN);
$processor->insertData("full_application", 17, $general);
if($processor->getConn()){
    list($general, $phones) = $processor->processGeneralInfo($_REQUEST['general_information']);
    $processor->insertData("full_application", 17, $general);
    echo $processor->getApplicationId();
}

我添加了一个新方法,并尝试打印它。我仍然得到文件404的错误。这真的很奇怪。我希望这有助于解决方案,因为我很难过。

0 个答案:

没有答案