Ver Mensaje Individual
  #1 (permalink)  
Antiguo 04/07/2013, 12:05
aesu
 
Fecha de Ingreso: noviembre-2011
Ubicación: Zulia/Maracaibo
Mensajes: 24
Antigüedad: 12 años, 5 meses
Puntos: 0
Exclamación Problema al obtener el ultimo id insertado(AYUDENME POR FAVOR)

Saludos a todos,
Estoy apenas comenzando a implementar las funciones de mysqli, ya que como se sabe estan declaradas obsoletas las funciones de mysql. Hasta cierto punto todo estaba muy bien pero cuando llego el momento de utilizar un metodo que creé para obtener el ultimo id insertado me arroja cero; he intentado solucionarlo de mil maneras pero sigo con el mismo problema.
Si alguien puede ayudarme se los agradezco, es que tengo poco tiempo para entregar la aplicacion y esto me tiene atrazado.

Aca les dejo el codigo.
Ahhh por cierto la aplicacion espara carga de cv, creo el usuario y necesito el último id de usuario ingresado para el resto de la información que se requiere.

Codigo



Clase base de datos
Código PHP:
Ver original
  1. <?php
  2.  
  3. abstract class AppDataBaseModel{
  4.  
  5.     private static $THE_HOST        = _APP_HOST_NAME_;
  6.     private static $THE_USER        = _APP_HOST_USER_;
  7.     private static $THE_PASSWORD    = _APP_HOST_PASSWORD_;
  8.     private static $THE_DATABASE    = _APP_HOST_DATABASE_;
  9.     private $dbConnection;
  10.     private $lastID;
  11.    
  12.  
  13.     protected function openConnection(){
  14.         $this->dbConnection = @new mysqli(self::$THE_HOST,
  15.                                           self::$THE_USER,
  16.                                           self::$THE_PASSWORD,
  17.                                           self::$THE_DATABASE);
  18.         if(mysqli_connect_errno()){
  19.             echo'Ha ocurrido un error al conectar con el servidor..!';
  20.         }
  21.        
  22.     }
  23.     private function closeConnection(){
  24.         $this->dbConnection->close();
  25.         //$this->dbConnection->free();
  26.     }
  27.     protected function executeQuery($mysqlQuery){
  28.         $this->openConnection();   
  29.         $queryResult = $this->dbConnection->query($mysqlQuery);
  30.         if(!$queryResult){
  31.             echo'Ha ocurrido un error al ejecutarse la consulta a la base de datos..!'.$this->dbConnection->error;
  32.         }else{
  33.             return $queryResult;
  34.         }      
  35.         $this->closeConnection();
  36.     }  
  37.     protected function countRows($mysqliQueryResult){
  38.         $this->openConnection();
  39.         $queryResult = $mysqliQueryResult->num_rows;
  40.         if($queryResult){
  41.             return $queryResult;
  42.         }else{
  43.             echo'Ha ocurrido un error al contar los registros de la tabla..!';
  44.         }
  45.         $this->closeConnection();
  46.     }  
  47.     protected function getLastID($theTable,$theId){
  48.         $this->openConnection();
  49.         $this->lastID = ''.$theId.'';    
  50.         $queryResult = $this->executeQuery("SELECT LAST_INSERT_ID() AS ".$this->lastID." FROM ".$theTable.";");
  51.         if($queryResult){
  52.             return $queryResult;
  53.         }else{
  54.             echo 'Ha ocurrido un error al intentar buscar el ultimo id insertado..!'.$this->dbConnection->error;
  55.         }
  56.         //$this->closeConnection();
  57.     }
  58.  
  59.         protected function escapeSqlString($fieldName){
  60.         $this->openConnection();
  61.         $queryResult = $this->dbConnection->real_escape_string($fieldName);
  62.         if($queryResult){
  63.             return $queryResult;
  64.         }else{
  65.             echo $this->dbConnection->error;
  66.         }
  67.         $this->closeConnection();
  68.     }
  69.    
  70.  
  71.     protected function mysqliFetchObject($param){
  72.         $this->openConnection();
  73.         $queryResult = $param->fetch_object();
  74.         return $queryResult;
  75.     }
  76.    
  77.     protected function mysqliFetchAssoc($param){
  78.         $this->openConnection();
  79.         $getResult = $param->fetch_assoc();
  80.         return $getResult;
  81.     }
  82.    
  83.     LENGUAJE EN CASO DE QUE CAMBIE EN EL FUTURO
  84.     protected function mysqliFetchArray($param){
  85.         $this->openConnection();
  86.         $getResult = $param->fetch_array();
  87.         return $getResult;
  88.     }
  89. }
  90.  
  91. ?>


Clase que maneja la información información del usuario

Código PHP:
Ver original
  1. <?php
  2. class UserPersonalInfo extends AppDataBaseModel{
  3.  
  4.     private $cinfoId;
  5.     private $infoUserId;
  6.     private $userInfoRif;
  7.     private $userInfoPassport;
  8.    
  9.     private $userInfoMaritalStatus;
  10.     private $userInfoBirthdate;
  11.     private $userInfoAge;
  12.  
  13.     private $userInfoLicenseType;
  14.     private $userInfoState;
  15.     private $userInfoCity;
  16.    
  17.     private $userInfoAddress;
  18.    
  19.     private $userInfoCountryCode;
  20.     private $userInfoStateCode;
  21.     private $userInfoPhoneNumber;
  22.    
  23.     private $userInfoCountryCode2;
  24.     private $userInfoStateCode2;
  25.     private $userInfoPhoneNumber2;
  26.    
  27.     private $tableName = _TABLE_USERS_INFO_;
  28.     private $tableName2 = _TABLE_USERS_;
  29.     private $tableName3 = _TABLE_USERS_LEVEL_ACCESS_;
  30.     private $theId ='i_users_personal_info_id';
  31.     private $userId ="u_user_id";
  32.     private $theUserId ='i_users_personal_info_user_id';
  33.     private $orderBy ='i_users_personal_info_id';
  34.    
  35.     public function __construct($infoId=0,$infoUserId=0,$userInfoRif="",$userInfoPassport="",$userInfoMaritalStatus="",$userInfoBirthdate="",$userInfoAge="",$userInfoLicenseType="",$userInfoState="",$userInfoCity="",$userInfoAddress="",$userInfoCountryCode="",$userInfoStateCode="",$userInfoPhoneNumber="",$userInfoCountryCode2="",$userInfoStateCode2="",$userInfoPhoneNumber2=""){
  36.  
  37.         $this->infoId = $infoId;
  38.         $this->infoUserId = $infoUserId;
  39.         $this->userInfoRif = $userInfoRif;
  40.         $this->userInfoPassport = $userInfoPassport;
  41.        
  42.         $this->userInfoMaritalStatus = $userInfoMaritalStatus;
  43.         $this->userInfoBirthdate = $userInfoBirthdate;
  44.         $this->userInfoAge = $userInfoAge;
  45.    
  46.         $this->userInfoLicenseType = $userInfoLicenseType;
  47.        
  48.         $this->userInfoState = $userInfoState;
  49.         $this->userInfoCity = $userInfoCity;
  50.         $this->userInfoAddress = $userInfoAddress;
  51.        
  52.         $this->userInfoCountryCode = $userInfoCountryCode;
  53.         $this->userInfoStateCode = $userInfoStateCode;
  54.         $this->userInfoPhoneNumber = $userInfoPhoneNumber;
  55.        
  56.         $this->userInfoCountryCode2 = $userInfoCountryCode2;
  57.         $this->userInfoStateCode2 = $userInfoStateCode2;
  58.         $this->userInfoPhoneNumber2 = $userInfoPhoneNumber2;
  59.     }//FIN DEL CONSTRUCTOR
  60.    
  61.     public function save(){
  62.         //AQUI ES DONDE LLAMO EL METODO GENERICO DE LA CLASE BASE DE DATOS PARA OBTENER EL ULTIMO ID
  63.         $resultQuery = $this->getLastID($this->tableName2,$this->userId);
  64.         $last_user_id = $this->mysqliFetchObject($resultQuery);
  65.         $this->userId = $last_user_id->u_user_id;
  66.         //LE COLOCO ESTE ECHO PARA VER SI ERROJA CERO O EL ID
  67.                 echo '<H1>ID - </H1>'.$this->userId;
  68.  
  69.         $getResult = $this->executeQuery("INSERT INTO ".$this->tableName."(i_users_personal_info_id,
  70.                                                                            i_users_personal_info_user_id,
  71.                                                                            i_users_personal_info_rif,
  72.                                                                            i_users_personal_info_passport,
  73.                                                                            i_users_personal_info_marital_status,
  74.                                                                            i_users_personal_info_birthdate,
  75.                                                                            i_users_personal_info_age,
  76.                                                                            i_users_personal_info_driving_license_type,
  77.                                                                            i_users_personal_info_state,
  78.                                                                            i_users_personal_info_city,
  79.                                                                            i_users_personal_info_address,
  80.                                                                            i_users_personal_info_phone_country_code,
  81.                                                                            i_users_personal_info_phone_state_code,                                         
  82.                                                                            i_users_personal_info_phone_number,                                         
  83.                                                                            i_users_personal_info_phone_country_code2,                                          
  84.                                                                            i_users_personal_info_phone_state_code2,
  85.                                                                            i_users_personal_info_phone_number2)
  86.                                             VALUES  ('',
  87.                                                '".$this->escapeSqlString($this->userId)."',
  88.                                                '".$this->escapeSqlString($this->userInfoRif)."',
  89.                                                '".$this->escapeSqlString($this->userInfoPassport)."',
  90.                                                '".$this->escapeSqlString($this->userInfoMaritalStatus)."',
  91.                                                '".$this->escapeSqlString($this->userInfoBirthdate)."',
  92.                                                '".$this->escapeSqlString($this->userInfoAge)."',
  93.                                                '".$this->escapeSqlString($this->userInfoLicenseType)."',
  94.                                                '".$this->escapeSqlString($this->userInfoState)."',
  95.                                                '".$this->escapeSqlString($this->userInfoCity)."',
  96.                                                '".$this->escapeSqlString($this->userInfoAddress)."',
  97.                                                '".$this->escapeSqlString($this->userInfoCountryCode)."',
  98.                                                '".$this->escapeSqlString($this->userInfoStateCode)."',
  99.                                                '".$this->escapeSqlString($this->userInfoPhoneNumber)."',
  100.                                                '".$this->escapeSqlString($this->userInfoCountryCode2)."',
  101.                                                '".$this->escapeSqlString($this->userInfoStateCode2)."',
  102.                                                '".$this->escapeSqlString($this->userInfoPhoneNumber2)."');");      
  103.         return $getResult;
  104.     }//FIN DEL METODO SAVE
  105.    
  106. }//FIN DE LA CLASE
  107.  
  108. ?>