2015-10-02 103 views
0

我正在使用PHP和和Slim框架爲REST API註冊用戶。 它給我一個錯誤,當我在先進的REST客戶端運行:完整性約束違規:1048列的'名稱'不能爲空

{"error":{"text":SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null}}

用於暫存模塊的代碼如下:

function insertUpdate() { 
    $request = \Slim\Slim::getInstance()->request(); 
    $update = json_decode($request->getBody()); 
    $sql = "INSERT INTO users (name,email,password,phoneNumber,imageUrl,created_at) VALUES (:name, :email, :password, :phoneNumber, :imageUrl, :created_at)"; 
    try { 
     $db = getDB(); 
     $stmt = $db->prepare($sql); 
     $stmt->bindParam("name", $update->name); 
     $stmt->bindParam("email", $update->email); 
     $stmt->bindParam("password",$update->password); 
     $stmt->bindParam("phoneNumber",$update->phoneNumber); 
     $stmt->bindParam("imageUrl",$update->imageUrl); 
     $time=time(); 
     $stmt->bindParam("created_at", $time); 

     $stmt->execute(); 
     echo "Register successfull"; 
    } catch(PDOException $e) { 
     //error_log($e->getMessage(), 3, '/var/tmp/php.log'); 
     echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    } 
} 

堆棧跟蹤:

#0 /Applications/XAMPP/xamppfiles/htdocs/api/v1/index.php(71): Slim\Slim::handleErrors(8, 'Trying to get p...', '/Applications/X...', 71, Array) 
#1 [internal function]: insertUpdate() 
#2 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Route.php(462): call_user_func_array('insertUpdate', Array) 
#3 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Slim.php(1326): Slim\Route->dispatch() 
#4 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Middleware/Flash.php(85): Slim\Slim->call() 
#5 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call() 
#6 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call() 
#7 /Applications/XAMPP/xamppfiles/htdocs/api/v1/Slim/Slim.php(1271): Slim\Middleware\PrettyExceptions->call() 
#8 /Applications/XAMPP/xamppfiles/htdocs/api/v1/index.php(14): Slim\Slim->run() 
#9 {main} 

如何解決此錯誤?

+3

'$最新情況:> name'是空的或空白的。如果它確實有價值,請嘗試回顯它。 – roullie

+0

@roullie我正在嘗試打印$ update-> name,但它給了我內部服務器錯誤 – Corrupt

+0

檢查您的日誌以查看錯誤所在的行和堆棧跟蹤。 – D4V1D

回答

0
function insertUpdate() { 
     $request = \Slim\Slim::getInstance()->request(); 
     parse_str($request->getBody(),$update); 
     $sql = "INSERT INTO users (name,email,password,phoneNumber,imageUrl,created_at) VALUES (:name, :email, :password, :phoneNumber, :imageUrl, :created_at)"; 
     try { 
      //Validate your filed before insert in db 
      if(!is_array($update) || (!$update)) { 
       throw new Exception('Invalid data received'); 
      } 
      // put your require filed list here 
      if((!$update['name']) || (!$update['email']) || (!$update['password'])){ 
       throw new Exception('Missing values for require fields'); 
      } 
      $db = getDB(); 
      $stmt = $db->prepare($sql); 
      $stmt->bindParam("name", $update['name']); 
      $stmt->bindParam("email", $update['email']); 
      $stmt->bindParam("password",$update['password']); 
      $stmt->bindParam("phoneNumber",$update['phoneNumber']); 
      $stmt->bindParam("imageUrl",$update['imageUrl']); 
      $time=time(); 
      $stmt->bindParam("created_at", $time); 

      $stmt->execute(); 
      echo "Register successfull"; 
     } catch(PDOException $e) { 
      //error_log($e->getMessage(), 3, '/var/tmp/php.log'); 
      echo '{"error":{"text":'. $e->getMessage() .'}}'; 
     }catch(Exception $e) { 
      //error_log($e->getMessage(), 3, '/var/tmp/php.log'); 
      echo '{"error":{"text":'. $e->getMessage() .'}}'; 
     } 
    } 

更新代碼按請求收到

+0

檢查您的$ request-> getBody()以及您如何調用API? –

+0

它給了我同樣的錯誤,我使用高級休息客戶端來檢查api – Corrupt

+0

註釋掉var_dump($ update);死;並再次檢查我有更新的代碼,以捕獲一般異常 –

相關問題