2016-07-23 95 views
-4

這是一個簡單的crud應用程序,使用slim框架我正在使用mysql數據庫... slim版本是2.6.2。 MySQL的版本是5.7.8如何使用slim獲取發佈請求數據?

郵遞員的形象:

enter image description here

// Adds a customer 
function addCustomer() { 
    //$request = Slim::getInstance()->request(); 
    $request = \Slim\Slim::getInstance()->request(); 
    $cus = json_decode($request->getBody()); 

    $sql = "INSERT INTO customers (Username,First_Name,Last_Name,Email,Phone,Password,Type,Status) VALUES (:username, :firstname, :lastname, :email, :phone, :password, :type, :status)"; 
    try { 
     $db = DB_Connection(); 
     $stmt = $db->prepare($sql); 

     $stmt->bindParam("username", $cus->Username); 
     $stmt->bindParam("firstname", $cus->First_Name); 
     $stmt->bindParam("lastname", $cus->Last_Name); 
     $stmt->bindParam("email", $cus->Email); 
     $stmt->bindParam("phone", $cus->Phone); 
     $stmt->bindParam("password", $cus->Password); 
     $stmt->bindParam("type", $cus->Type); 
     $stmt->bindParam("status", $cus->Status); 
     $stmt->execute(); 
     $cus->id = $db->lastInsertId(); 
     $db = null; 
     echo json_encode($cus); 
    } catch(PDOException $e) { 
     echo '{"error":{"text":'. $e->getMessage() .'}}'; 
    } 
} 

enter image description here

Get request is working fine

Get請求是工作的罰款:

+0

也許你傳遞一個空$ cus->用戶名。 – msantos

+0

是的,我也認爲 – arnoldssss

+0

'$ cus = json_decode($ request-> getBody());''''''''可以請你發佈輸出'var_dump($ cus)' –

回答

1

我相信你'回覆missing是綁定語句上的參數之前的冒號。您應該修正爲:

$stmt->bindParam(":username", $cus->Username); 
$stmt->bindParam(":firstname", $cus->First_Name); 
$stmt->bindParam(":lastname", $cus->Last_Name); 
$stmt->bindParam(":email", $cus->Email); 
$stmt->bindParam(":phone", $cus->Phone); 
$stmt->bindParam(":password", $cus->Password); 
$stmt->bindParam(":type", $cus->Type); 
$stmt->bindParam(":status", $cus->Status); 

編輯

其實現在,我花了一個更深層面上來看,你傳遞的PARAMS用首字母小寫(看看郵遞員截圖中,您使用的用戶名,但在你的代碼中,你正試圖分配用戶名)。只要把一個大寫的「用戶名」作爲一個後場郵差,或更換如下:

$stmt->bindParam(":username", $cus->username); 
$stmt->bindParam(":firstname", $cus->firstname); 
$stmt->bindParam(":lastname", $cus->lastname); 
$stmt->bindParam(":email", $cus->email); 
$stmt->bindParam(":phone", $cus->phone); 
$stmt->bindParam(":password", $cus->password); 
$stmt->bindParam(":type", $cus->type); 
$stmt->bindParam(":status", $cus->status); 

編輯II

替換爲以下循環中的所有綁定:

foreach($request->params() as $key => $val) { 
    $stmt->bindParam($key, $val); 
} 
+1

驅動程序自動添加冒號(如果不存在)。 – chris85

+0

我替換你的綁定語句,但我仍然得到相同的錯誤 – arnoldssss

+1

這是因爲驅動程序自動添加':'。這個答案什麼都不加。 https://github.com/php/php-src/blob/PHP-5.3.24/ext/pdo/pdo_stmt.c#L363(雖然爲了將來的兼容性,你應該有':',那個功能不在手冊) – chris85

1

我認爲$ cus->你試圖插入的用戶名是empty.Try使用$ stmt-> bindValue而不是$ stmt-> bindParam。

+0

好吧,讓我試試 – arnoldssss

+0

我得到一個熟悉的錯誤,如前一個:類型:ErrorException 代碼:8 消息:試圖讓非物件的財產 – arnoldssss

+0

你打印過用戶名值嗎? – msantos

1

這看起來像是一個區分大小寫的問題。您從表單中綁定參數,試圖通過對應的數據庫列名引用它們。你的郵差截圖顯示輸入名稱全部爲小寫。

嘗試

$stmt->bindParam("username", $cus->username); 
    $stmt->bindParam("firstname", $cus->firstname); 
    $stmt->bindParam("lastname", $cus->lastname); 
    $stmt->bindParam("email", $cus->email); 
    $stmt->bindParam("phone", $cus->phone); 
    $stmt->bindParam("password", $cus->password); 
    $stmt->bindParam("type", $cus->type); 
    $stmt->bindParam("status", $cus->status); 
+0

好吧..像以前一樣使用郵遞員和您的代碼仍然給我同樣的錯誤.. {「error」:{「text」:SQLSTATE [23000]:完整性約束衝突:1048'用戶名'不能爲空}} 謝謝.. – arnoldssss

相關問題