如何挑选 DTO 对象的值以用于 sql 更新

How to pick out values on DTO object to use in sql update

首先,解释一下我在做什么:我正在制作一个带有 ajax 更新公司信息的请求(在表单提交按钮上)的简单表单,我是新手到 php 但想在 ajax 中使用数据传输对象(就像我之前在其他语言中所做的那样)但我没有走得太远

好的,这可能是一个非常简单的问题。就像我上面说的,我以前使用过 AJAX 数据传输对象,只是没有使用 php... 我有点沮丧,因为我似乎无法选择要使用的对象数据在目标页面上我的 sql 更新中。我可以在 console.log 中看到该对象似乎完全形成并包含数据,但我终其一生都无法获得正确的语法来挑选数据!帮助

Ajax请求:

    var $CompanyName = $("#CompanyName").val(),
        $TelNumberAreaCode = $("#TelNumberAreaCode").val(),
        $TelNumber = $("#TelNumber").val(),
        $EmailAddress = $("#EmailAddress").val(),
        $AddressLine1 = $("#AddressLine1").val(),
        $AddressLine2 = $("#AddressLine2").val(),
        $AddressCity = $("#AddressCity").val(),
        $AddressCounty = $("#AddressCounty").val(),
        $AddressPostCode = $("#AddressPostCode").val();

        var CompanyDetailsDTO = {};
        CompanyDetailsDTO.CompanyName = $CompanyName;
        CompanyDetailsDTO.TelNumberAreaCode = $TelNumberAreaCode;
        CompanyDetailsDTO.TelNumber = $TelNumber;
        CompanyDetailsDTO.EmailAddress = $EmailAddress;
        CompanyDetailsDTO.AddressLine1 = $AddressLine1;
        CompanyDetailsDTO.AddressLine2 = $AddressLine2;
        CompanyDetailsDTO.AddressCity = $AddressCity;
        CompanyDetailsDTO.AddressCounty = $AddressCounty;
        CompanyDetailsDTO.AddressPostCode = $AddressPostCo;

        var DTO = { 'request': CompanyDetailsDTO }

        $.ajax({
              type: "POST",
              url: "php/updates/update-businessdetails.php",
              data: JSON.stringify(DTO),
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: CompanyDetailsSuccess,
              error: CompanyDetailsError
          }); 

PHP更新/目标文件代码:

<?php 
 // DB Connection

 // How can i pick the below vars out the object???
 // I've tried loads of different syntaxes that i think should/could be right
 $CompanyName = ??
 $TelNumberAreaCode = ??
 $TelNumber = ??
 $EmailAddress = ??
 $AddressLine1 = ??
 $AddressLine2 = ??
 $AddressCity = ??
 $AddressCounty = ??
 $AddressPostCo = ??


 $sql="UPDATE `businessdetails` SET `TelNumberAreaCode` = '$TelNumberAreaCode',`EmailAddress` = '$EmailAddress',`AddressLine1` = '$AddressLine1',`AddressLine2` = '$AddressLine2',`AddressCity` = '$AddressCity',`AddressCounty` = '$AddressCounty',`TelNumber` = '$TelNumber',`AddressPostCode` = '$AddressPostCode',`CompanyName` =    '$CompanyName'";

$result = $conn->query($sql);

if ($result)
 {
  header('Content-Type: application/json');
  print json_encode($result);
 }
?>

我一定是做错了什么,或者是愚蠢的,因为我似乎也找不到答案?!

尝试使用json_decode():

// @var stdClass 
$decoded_data = json_decode(file_get_contents('php://input'), false);

// And access its properties:
$CompanyName = $decoded_data->request->CompanyName;
$TelNumberAreaCode = $decoded_data->request->TelNumberAreaCode;
// ...