2016-08-11 7 views
1

私はPHPを使用してデータを保存しようとしています。これは私が試した方法です:PHPで非オブジェクトエラーのプロパティを取得しようとしています

<?php 
session_start(); 
if(!isset($_SESSION["MemberID"])) header("Location:logout.php"); 
include '../config.php'; 
include '../momdb.php'; 

$page_title = "Customers"; 
$mode = "Save"; 
$db = 'db'; 
$showmessage = false; 
$message = ""; 
if (isset ($_POST ["btnSave"])) { 
    $data = new table(); 
    $data->MomID = $_POST ["CustomerID"]; 
    $data->CustomerName = $_POST ["CustomerName"]; 
    $data->FromDate = string_to_date ($_POST ["FromDate"]); 
    $data->ToDate = string_to_date ($_POST ["ToDate"]); 
    $data->CreatedBy = $_SESSION["MemberID"]; 
    $data->CreatedDatetime = date ("Y-m-d H:i:s"); 
    $data->LastModifiedBy = $_SESSION["MemberID"];; 
    $data->LastModifiedDatetime = date ("Y-m-d H:i:s"); 

    $rows = array(); 
    if (intval ($data->CustomerID) > 0) { 
     $rows = $db::Update ($data); 
    } else { 
     $rows = $db::Save ($data); 
    } 
    if (intval ($rows ["WebID"]) > 0) { 
     $date = new DateTime(); 
     header ("Location: customerlist.php?msg=yes&msgt=s&t=" . $date->getTimestamp() . "&mtext=Record saved successfully"); 
    } else { 
     $showmessage = true; 
     $message = $rows ["Message"]; 
    } 
} elseif (isset ($_GET ["CustomerID"])) { 
    $data = $db::GetByID ($_GET ["CustomerID"]); 
    if ($data == false) { 
     $data = new table(); 
     $data->CustomerID = 0; 
    } 
} 

if ($data->CustomerID == "0") 
    $mode = "Save"; 
else 
    $mode = "Update"; 
?> 

このコードは、私の値をDBページに保存してDBに保存するためのものです。私はPDOメソッドを使用しました。この方法で記録を保存するのは初めてです。そういうわけで、混乱がたくさんあります。次のコードは、値をDBに保存するために使用したSave()メソッドを示しています。

public function Save(table $customer) { 
     $data = new DB(); 
     $stmt = $data->connection->prepare ("CALL proc_CreateCustomer (:CustomerName, :FromDate,:ToDate,:CreatedBy, :CreatedDatetime, :LastModifiedBy, :LastModifiedDatetime)"); 
     $stmt->bindParam (':CustomerName', trim ($customer->CustomerName)); 
     $stmt->bindParam (':FromDate', $customer->FromDate); 
     $stmt->bindParam (':ToDate', $customer->ToDate); 
     $stmt->bindParam (':CreatedBy',$customer->CreatedBy); 
     $stmt->bindParam (':CreatedDatetime', $customer->CreatedDatetime); 
     $stmt->bindParam (':LastModifiedBy',$customer->LastModifiedBy); 
     $stmt->bindParam (':LastModifiedDatetime', $customer->LastModifiedDatetime); 
     $stmt->execute(); 
     $record = $stmt->fetch (PDO::FETCH_OBJ); 
     if ($record == false) { 
      return array (
        "WebID" => 0, 
        "Message" => $record->Message, 
        "AppID" => $customer->AndroidID 
      ); 
     } else { 
      return array (
        "WebID" => $record->CustomerID, 
        "Message" => $record->Message, 
        "AppID" => $customer->AndroidID 
      ); 
     } 
    } 

これは私が試した方法です。しかし、詳細を保存しようとすると、「Trying to get property of non-object in D:\Workspace\Application\momdb.php on line 39」と表示されます。つまり、"Message" => $record->Message,"AppID" => $customer->AndroidIDというエラーが表示されます。私はこれに何が間違っているのではありません。誰かが私はそれがうまく動作するように私は何を変えなければならないと教えてくれる?コード$recordのこの部分で

+0

print_r($ record)を使用してここに結果を投稿してください。 – coder

+0

私は誤解を招くかもしれませんが、メッセージがあなたのstmt呼び出しに存在しないように見えます(proc_CreateCustomerから来ない場合)。 – Dave

答えて

1
if ($record == false) { 
    return array (
      "WebID" => 0, 
      "Message" => $record->Message, 
      "AppID" => $customer->AndroidID 
    ); 
} 

偽(Y)であるので、$record->Messageは存在しません。

関連する問題